Use decorators to simplify using UI creation flow for PySide
Project description
pyside-callbacks
GitHub repository: https://github.com/schang412/pyside-callbacks
A small library that provides utility decorators for simplifying the creation of PySide6 UI. This package also contains a mypy plugin to assist in the type-checking the signals according to the parameters.
The QT Designer workflow with Python would look like this:
- Use QT Designer to create
main_win.ui
file. - Use
uic
to compilemain_win.ui
intomain_win.py
- Sub-Class the class defined in
main_win.py
. - Connect the signals to their handlers.
import main_win
from PySide6 import QtWidgets
class MyQtApp(main_win.Ui_MainWindow, QtWidgets.QMainWindow):
def __init__(self) -> None:
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.line_edit_return_pressed)
self.lineEdit.returnPressed.connect(self.line_edit_return_pressed)
def line_edit_return_pressed(self) -> None:
cmd = self.lineEdit.text()
if not cmd:
return
self.lineEdit.setText("")
self.display.appendPlainText(cmd)
However, this connection method does not inherently offer type-checking and could be improved using decorators:
import main_win
from PySide6 import QtWidgets
import pyside_callbacks
@pyside_callbacks.pyside_callbacks
class MyQtApp(main_win.Ui_MainWindow, QtWidgets.QMainWindow):
def __init__(self) -> None:
super().__init__()
self.setupUi(self)
@pyside_callbacks.widget_event("pushButton", "clicked")
@pyside_callbacks.widget_event("lineEdit", "returnPressed")
def line_edit_return_pressed(self) -> None:
cmd = self.lineEdit.text()
if not cmd:
return
self.lineEdit.setText("")
self.display.appendPlainText(cmd)
Note that we need to decorate both the class and the method because we need to add a hook to the
__init__
method in order to register the callback to the class instance. The way that we keep track of the callbacks requires that thewidget_event
decorator is the outermost decorator. However, currently, themypy
plugin expects onlywidget_event
callbacks on functions that use it.In other words, we cannot mix @widget_event with other decorators (for example, @staticmethod).
We can also include a mypy
plugin to ensure that our signals are correct. We add the pyside_callbacks_mypy
plugin and suppress the errors from the uic
generated file.
[tool.mypy]
plugins = [
"pyside_callbacks_mypy.plugin"
]
[[tool.mypy.overrides]]
module = "main_win"
ignore_errors = true
Adding the following lines to the example application:
@pyside_callbacks.widget_event("lineEdit", "cursorPositionChanged")
def curpos_changed(self, b: str) -> None:
print("changed cursor position!")
Then, running mypy
we will find the errors:
example/my_app/app.py:34: error: Argument 2 to "curpos_changed" has incompatible type "str"; Emitted signal will expect type "int". [arg-type]
example/my_app/app.py:34: error: Too many arguments for "curpos_changed"; Emitted signal will supply ["int", "int"] [call-arg]
Found 2 errors in 1 file (checked 2 source files)
Note that we have to add a type-hint to
main_win.Ui_MainWindow.setupUi
otherwise dynamic types (typing.Any) will be inferred for all the widgets.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pyside-callbacks-0.1.0.tar.gz
.
File metadata
- Download URL: pyside-callbacks-0.1.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.4.9 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a3a5f68a0afe4654a6732c16170f3a5e838a61c113bf60a4d66c3fa44592e83 |
|
MD5 | 4b6ac9d2c3c4aae8eb298207444daa2c |
|
BLAKE2b-256 | dab12bdc00241c7fc098b42445079ac98b2f243392a1b5d11f3d8ca728680f4b |
File details
Details for the file pyside_callbacks-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: pyside_callbacks-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.4.9 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61f7aaff1371730645cb0f175ed0f6aa1f7358dafa6fd72e70bf19d4180f9e93 |
|
MD5 | 2617e63d24aece5b2592f8b486e3d61d |
|
BLAKE2b-256 | 796fc75d00f6f9583d61b17ddc11a5ca6183097ccb2597fb83623231cd5d0d7a |