Python desktop application example

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Learn to create a desktop app with Python and Qt

pyqt/examples

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

These PyQt examples show you how to create a desktop app with Python and Qt. Start with «Hello World» or browse the official PyQt demos. You can run every example yourself on Windows, Mac or Linux. All you need is Python 3. For instructions, please see below.

These examples are taken from the following book:

The PyQt source archive also contains a large number of sample files. You can find them reproduced here in the src/pyqt-official directory. The easiest way to start them is to follow the instructions about running examples below, then execute the following commands:

cd src/pyqt-official/qtdemo python qtdemo.py 

This starts the PyQt example launcher:

PyQt Examples launcher

You can use it to easily browse and run the official demo applications. The following examples are quite nice for instance:

  • Quick / Animation / ColorAnimation
  • Graphics Effects / Lighting and Shadows
  • Desktop / System Tray
  • Desktop / Screenshot
  • Widgets / Tetrix

Running the examples is really easy. The only thing you need is Python 3.

First, download the ZIP archive of this repository and unpack it. Open a command prompt and use cd to navigate into the top-level directory of the archive.

Create a virtual environment via the command:

This creates the folder venv/ in your current directory. It will contain the necessary libraries for running the examples.

To activate the virtual environment, use the following command:

# On Windows: call venv\Scripts\activate.bat # On Mac / Linux: source venv/bin/activate 

Now execute the following to install the necessary dependencies:

pip install -Ur src/requirements.txt 

Once you have done this, use cd to navigate to the example you’re interested in in the src/ folder. For example:

You’ll find a .py file there, typically main.py . You can run it with the command:

Please note that the virtual environment must still be active for this to work.

This repository uses PyQt6 to use Qt from Python. Another, alternative binding is PySide6 (also called «Qt for Python»). It is less mature than PyQt6 but has the advantage that you can use it for free in commercial projects.

If you want to use PySide6 instead of PyQt6, simply replace all mentions of the latter by the former. For instance, in src/requirements.txt , replace PyQt6 by PySide6 . Similarly for any code examples: from PyQt6.QtWidgets . becomes from PySide6.QtWidgets . etc.

Alternatively, if you don’t want to commit to either of the two bindings at this stage, you can also use Qt.py. This is an abstraction over PySide6 and PyQt6. It loads whichever of the two bindings is available. To use it for the examples presented here, replace all mentions of PyQt6 by just Qt .

Except where otherwise indicated, you may use the source code of examples 1 — 15 in the src/ directory under the terms of the MIT or GPLv3 licenses.

The screenshots in this repository may be used under the terms of the CC BY-NC-SA 4.0 if you prominently mention and link to Michael Herrmann’s PyQt6 book.

Источник

Creating desktop applications in Python

Desktop applications are software programs that run on a computer’s desktop and provide functionality to the user. These applications can range from simple programs like text editors to more complex ones like video editing software. In this article, we will explore how to create desktop applications using Python, a popular programming language known for its simplicity and ease of use.

To get started, we first need to install a GUI (graphical user interface) library for Python. There are several libraries available, but two of the most popular ones are Tkinter and PyQt. Tkinter is a built-in library that comes with Python and is easy to use, but it may not have as many features as PyQt. PyQt, on the other hand, is a third-party library that offers more advanced features but can be more difficult to learn.

Here is an example of a simple Tkinter-based desktop application that displays a window with a button:

import tkinter as tk def on_button_click(): print("Button clicked!") root = tk.Tk() button = tk.Button(root, text="Click me!", command=on_button_click) button.pack() root.mainloop() 

In this example, we first import the Tkinter library. Next, we define a function called «on_button_click» that will be called when the button is clicked. We then create a Tkinter window (root) and a button widget, setting its text to «Click me!» and its command to the «on_button_click» function. Finally, we call the «mainloop» method on the root window to start the application.

Here is an example of a more advanced PyQt-based desktop application that displays a window with a button and a text field:

import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit, QPushButton class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("My Application") self.resize(400, 300) self.text_field = QLineEdit(self) self.text_field.move(20, 20) self.button = QPushButton("Click me!", self) self.button.move(20, 60) self.button.clicked.connect(self.on_button_click) def on_button_click(self): print("Button clicked!") print("Text field value:", self.text_field.text()) app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) 

In this example, we first import the necessary modules from the PyQt5 library. Next, we create a MainWindow class that inherits from QMainWindow. We define a constructor that sets the window title, size and widgets. We added QLineEdit and QPushButton widgets to the window. We have also connected the button’s «clicked» signal to a function that will be called when the button is clicked. Finally, we create an instance of QApplication and run the application.

These examples demonstrate the basics of creating desktop applications using Python and Tkinter/PyQt. While they are simple, they serve as a good starting point for building more complex applications. With Tkinter and PyQt, you can create windows, buttons, text fields, and other widgets, and customize their appearance and behavior using a wide range of options and methods. You can also create menus, dialogs, and other UI elements, as well as handle user input and events.

In addition to Tkinter and PyQt, there are other libraries and frameworks that can be used to create desktop applications with Python. Some popular alternatives include wxPython, PyGTK, and PySide. Each of these libraries has its own strengths and weaknesses, and the choice of library will depend on your specific requirements and the type of application you are building.

When building desktop applications with Python, it is also important to consider the packaging and distribution of your application. Python provides tools such as cx_Freeze and PyInstaller that can be used to create standalone executables, which can be distributed to users without the need to install a Python interpreter.

In summary, Python is a powerful and versatile language that can be used to create a wide range of desktop applications. With libraries such as Tkinter and PyQt, it is easy to create UI elements, handle user input and events, and customize the appearance and behavior of your application. Whether you are a beginner or an experienced developer, Python can be a great choice for building desktop applications.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

15 minute (small) desktop apps built with PyQt

License

pythonguis/15-minute-apps

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A collection of 15 small — minute — desktop applications written in Python using the PyQt framework. These apps are intended as examples from which you can poke, hack and prod your way to writing your own tools.

Many of these apps have more detailed write-ups on my PyQt5/PySide2 site at LearnPyQt.com. If you’re new to creating GUI apps check out the introductory pyqt5 tutorial.

The apps showcase various parts of the Qt framework, including advanced widgets, multimedia, graphics views and decorationless windows. However, the most generally interesting/feature complete applications are Minesweeper, Solitaire and Paint.

  1. Web Browser (untabbed) — «MooseAche»
  2. Web Browser (tabbed) — «Mozzarella Ashbadger»
  3. Minesweeper — «Moonsweeper»
  4. Notepad — «No2Pads»
  5. Calculator — «Calculon» (QtDesigner)
  6. Word Processor — «Megasolid Idiom»
  7. Webcam/Snapshot — «NSAViewer»
  8. Media Player — «Failamp»
  9. Post-it Notes — «Brown Note» (QtDesigner)
  10. Paint — «Piecasso» (QtDesigner)
  11. Unzip — «7Pez» (QtDesigner)
  12. Translator — «Translataarrr» (QtDesigner)
  13. Weather — «Raindar» (QtDesigner)
  14. Currency converter — «Doughnut» (PyQtGraph)
  15. Solitaire — «Ronery» (QGraphicsScene)

To use each app you first need to install the requirements. In most cases the only requirements are PyQt5, and occasionally requests. To install app-specific requirements change to the folder of the app and run:

pip3 install -r requirements.txt 

Once the requirements are installed, you can run the app using Python 3.

The application window should appear.

Want to build your own apps?

If you think these apps are neat and want to learn more about PyQt in general, take a look at my PyQt5 tutorial which covers everything you need to know to start building your own applications with PyQt.

You can also find write-ups about these «minute apps» on the same site.

All code is licensed under an MIT license. This allows you to re-use the code freely, remixed in both commercial and non-commercial projects. The only requirement is to include the same license when distributing.

Icons used in the applications are by Yusuke Kamiyaman.

Источник

Читайте также:  Html вертикальное выравнивание во всей таблице
Оцените статью