Skip to content

Commit c0adffc

Browse files
committed
Integrate unofficial examples with screenshots
1 parent 2646734 commit c0adffc

File tree

90 files changed

+2185
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+2185
-2
lines changed

README.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,68 @@
1-
# examples
2-
Examples files for PyQt5
1+
# PyQt examples: Desktop GUIs based on PyQt5
2+
3+
These PyQt examples teach you to create a desktop app with Python. Start with "Hello World" and work your way up to the official [demos](src/pyqt-official) that ship with PyQt.
4+
5+
The screenshots below were taken on Ubuntu Linux. You can also use Windows or macOS.
6+
7+
| <a href="src/01 PyQt QLabel"><img src="src/01 PyQt QLabel/pyqt-qlabel.png" alt="PyQt QLabel" width=100px></a> | <a href="src/02 PyQt Widgets"><img src="src/02 PyQt Widgets/pyqt-widgets.png" alt="PyQt widgets screenshot" width=200px></a> | <a href="src/03 QVBoxLayout PyQt5"><img src="src/03 QVBoxLayout PyQt5/qvboxlayout-pyqt5.png" alt="QVBoxLayout PyQt5" width=100px></a> | <a href="src/04 PyQt Signals and Slots"><img src="src/04 PyQt Signals and Slots/pyqt-signals-and-slots.jpg" alt="PyQt Signals and Slots" width=170px></a> | <a href="src/05 Qt Designer Python"><img src="src/05 Qt Designer Python/qt-designer-windows.png" alt="Qt Designer Python" width=190px></a> |
8+
| :--: | :--: | :--: | :--: | :--: |
9+
| <a href="src/01 PyQt QLabel">Hello World!</a> | <a href="src/02 PyQt Widgets">Common PyQt Widgets</a> | <a href="src/03 QVBoxLayout PyQt5">Layouts</a> | <a href="src/04 PyQt Signals and Slots">Signals and Slots</a> | <a href="src/04 Qt Designer Python">Qt Designer & Python</a> |
10+
11+
| <a href="src/06 QML Python example"><img src="src/06 QML Python example/qml-python-example.png" alt="QML Python example" width=200px></a> | <a href="src/07 Qt Text Editor"><img src="src/07 Qt Text Editor/screenshots/qt-text-editor.png" alt="Qt Text Editor" width=180px></a> | <a href="src/08 PyQt5 exe"><img src="src/08 PyQt5 exe/pyqt5-exe.png" alt="PyQt5 exe" width=213px></a> | <a href="src/09 Qt dark theme"><img src="src/09 Qt dark theme/qt-dark-theme.png" alt="Qt dark theme" width=180px></a> |
12+
| :--: | :--: | :--: | :--: |
13+
| <a href="src/06 QML Python example">QML Python example</a> | <a href="src/07 Qt Text Editor">Qt Text Editor</a> | <a href="src/08 PyQt5 exe">Packaging & deployment</a> | <a href="src/09 Qt dark theme">Qt Dark Theme</a> |
14+
15+
| <a href="src/10 QPainter Python example"><img src="src/10 QPainter Python example/qpainter-python-example.png" alt="QPainter Python example" width=200px></a> | <a href="src/11 PyQt Thread example"><img src="src/11 PyQt Thread example/pyqt-thread-example.png" alt="PyQt Thread example" width=175px></a> | <a href="src/12 QTreeView example in Python"><img src="src/12 QTreeView example in Python/qtreeview-example-in-python.png" alt="QTreeView example in Python" width=260px></a> | <a href="src/13 PyQt5 QListView"><img src="src/13 PyQt5 QListView/pyqt5-qlistview.png" alt="PyQt5 QListView" width=138px></a> |
16+
| :--: | :--: | :--: | :--: |
17+
| <a href="src/10 QPainter Python example">Action Shooter</a> | <a href="src/11 PyQt Thread example">Chat Client</a> | <a href="src/12 QTreeView example in Python">Tree Views</a> | <a href="src/13 PyQt5 QListView">Lists</a> |
18+
19+
| <a href="src/14 QAbstractTableModel example"><img src="src/14 QAbstractTableModel example/qabstracttablemodel-example.png" alt="QAbstractTableModel example" height=120px></a> | <a href="src/15 PyQt database example"><img src="src/15 PyQt database example/pyqt-database-example.png" alt="QAbstractTableModel example" height=120px></a> |
20+
| :--: | :--: |
21+
| <a href="src/14 QAbstractTableModel example">Custom Tables</a> | <a href="src/15 PyQt database example">PyQt database example</a> |
22+
23+
## Running the examples
24+
25+
Running the examples is really easy. The only thing you need is [Python 3](https://www.python.org/downloads/).
26+
27+
First, download the [ZIP archive of this repository](https://github.com/pyqt/examples/archive/master.zip) and unpack it. Open a command prompt and use `cd` to navigate into the top-level directory of the archive.
28+
29+
Create a virtual environment via the command:
30+
31+
python3 -m venv venv
32+
33+
This creates the folder `venv/` in your current directory. It will contain the necessary libraries for running the examples.
34+
35+
To activate the virtual environment, use the following command:
36+
37+
```
38+
# On Windows:
39+
call venv\Scripts\actviate.bat
40+
# On Mac / Linux:
41+
source venv/bin/activate
42+
```
43+
44+
Now execute the following to install the necessary dependencies:
45+
46+
pip install -Ur src/requirements.txt
47+
48+
Once you have done this, use `cd` to navigate to the example you're interested in in the [`src/`](src) folder. For example:
49+
50+
cd "src/01 PyQt QLabel"
51+
52+
You'll find a `.py` file there, typically `main.py`. You can run it with the command:
53+
54+
python main.py
55+
56+
Please note that the virtual environment must still be active for this to work.
57+
58+
## Using PySide2
59+
60+
This repository uses PyQt5 to use Qt from Python. Another, alternative binding is PySide2 (also called "Qt for Python"). It is less mature than PyQt5 but has the advantage that you can use it for free in commercial projects.
61+
62+
If you want to use PySide2 instead of PyQt5, simply replace all mentions of the latter by the former. For instance, in [`src/requirements.txt`](src/requirements.txt), replace `PyQt5` by `PySide2`. Similarly for any code examples: `from PyQt5.QtWidgets ...` becomes `from PySide2.QtWidgets ...` etc.
63+
64+
Alternatively, if you don't want to commit to either of the two bindings at this stage, you can also use [Qt.py](https://github.com/mottosso/Qt.py). This is an abstraction over PySide2 and PyQt5. It loads whichever of the two bindings is available. To use it for the examples presented here, replace all mentions of `PyQt5` by just `Qt`.
65+
66+
## License
67+
68+
Except where otherwise indicated, the contents here are © me, Michael Herrmann. I'm happy for you to use the source code under the terms of the MIT license. The screenshots may be used under the terms of the [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/).

src/.gitignore

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
develop-eggs/
12+
dist/
13+
downloads/
14+
eggs/
15+
.eggs/
16+
lib/
17+
lib64/
18+
parts/
19+
sdist/
20+
var/
21+
wheels/
22+
*.egg-info/
23+
.installed.cfg
24+
*.egg
25+
MANIFEST
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*.cover
46+
.hypothesis/
47+
.pytest_cache/
48+
49+
# Translations
50+
*.mo
51+
*.pot
52+
53+
# Django stuff:
54+
*.log
55+
local_settings.py
56+
db.sqlite3
57+
58+
# Flask stuff:
59+
instance/
60+
.webassets-cache
61+
62+
# Scrapy stuff:
63+
.scrapy
64+
65+
# Sphinx documentation
66+
docs/_build/
67+
68+
# PyBuilder
69+
target/
70+
71+
# Jupyter Notebook
72+
.ipynb_checkpoints
73+
74+
# pyenv
75+
.python-version
76+
77+
# celery beat schedule file
78+
celerybeat-schedule
79+
80+
# SageMath parsed files
81+
*.sage.py
82+
83+
# Environments
84+
.env
85+
.venv
86+
env/
87+
venv/
88+
ENV/
89+
env.bak/
90+
venv.bak/
91+
92+
# Spyder project settings
93+
.spyderproject
94+
.spyproject
95+
96+
# Rope project settings
97+
.ropeproject
98+
99+
# mkdocs documentation
100+
/site
101+
102+
# mypy
103+
.mypy_cache/
104+
105+
*.qmlc

src/01 PyQt QLabel/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# PyQt QLabel
2+
3+
This example shows how you can create a Hello World app using PyQt. It uses a [`QLabel`](https://doc.qt.io/qt-5/qlabel.html) to display a simple window:
4+
5+
![PyQt QLabel screenshot](pyqt-qlabel.png)
6+
7+
```
8+
from PyQt5.QtWidgets import *
9+
app = QApplication([])
10+
label = QLabel('Hello World!')
11+
label.show()
12+
app.exec_()
13+
```
14+
15+
For instructions how you can run this code, please see the [top-level README](https://github.com/1mh/pyqt-examples#running-the-examples).
16+
17+
The code works as follows: First, we import the necessary PyQt classes via the statement:
18+
19+
from PyQt5.QtWidgets import *
20+
21+
Next, we create a [`QApplication`](https://doc.qt.io/Qt-5/qapplication.html). This is required in every PyQt app. In a sense, it initializes PyQt:
22+
23+
app = QApplication([])
24+
25+
Then, we create the label with the text we want:
26+
27+
label = QLabel('Hello World!')
28+
29+
By calling `.show()` on a [widget](../02%20PyQt%20Widgets), we can spawn a window that displays it:
30+
31+
label.show()
32+
33+
Finally, we hand control over to Qt:
34+
35+
app.exec_()
36+
37+
This too is required in every Qt application. It gives Qt a chance to run and process user input, such as for instance when the user clicks the "Window close" button.
38+
39+
And that's it! Congratulations on your first PyQt app :-)

src/01 PyQt QLabel/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from PyQt5.QtWidgets import *
2+
app = QApplication([])
3+
label = QLabel('Hello World!')
4+
label.show()
5+
app.exec_()

src/01 PyQt QLabel/pyqt-qlabel.png

4 KB
Loading

src/02 PyQt Widgets/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# PyQt Widgets
2+
3+
A *widget* is a GUI element: A button, a text field, ... The sample application in this directory shows the most common PyQt widgets:
4+
5+
![PyQt widgets screenshot](pyqt-widgets.png)
6+
7+
If you know HTML: Widgets are a little like HTML elements. They can be nested, and have a different appearance and behavior depending on their type. (Eg. a link `<a>` looks and behaves differently from an image `<img>`.)
8+
9+
Some of the widgets you can see in this screenshot are:
10+
11+
* [QLabel](https://doc.qt.io/qt-5/qlabel.html)
12+
* [QComboBox](https://doc.qt.io/qt-5/qcombobox.html)
13+
* [QCheckBox](https://doc.qt.io/qt-5/qcheckbox.html)
14+
* [QRadioButton](https://doc.qt.io/qt-5/qradiobutton.html)
15+
* [QPushButton](https://doc.qt.io/qt-5/qpushbutton.html)
16+
* [QTableWidget](https://doc.qt.io/qt-5/qtablewidget.html)
17+
* [QLineEdit](https://doc.qt.io/qt-5/qlineedit.html)
18+
* [QSlider](https://doc.qt.io/qt-5/qslider.html)
19+
* [QProgressBar](https://doc.qt.io/qt-5/qprogressbar.html)
20+
21+
The source code for this application is in [`main.py`](main.py). For instructions how to run it, please see [here](https://github.com/1mh/pyqt-examples#running-the-examples). Don't worry if you don't yet fully understand the source code. The main purpose of this example is to give you a feel for what a widget is, and which ones are available. The next examples give you a more gradual route to more advanced PyQt topics.

0 commit comments

Comments
 (0)