Skip to main content

pyside-callbacks

Actions status pdm-managed pre-commit

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:

  1. Use QT Designer to create main_win.ui file.
  2. Use uic to compile main_win.ui into main_win.py
  3. Sub-Class the class defined in main_win.py.
  4. 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 the widget_event decorator is the outermost decorator. However, currently, the mypy plugin expects only widget_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.

Metadata

Release files for pyside-callbacks 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyside-callbacks 0.1.1
File Size Uploaded
pyside-callbacks-0.1.1.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyside-callbacks 0.1.1
File Interpreter ABI Platform
pyside_callbacks-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.0 kB

Release files / pyside-callbacks-0.1.1.tar.gz

Download URL pyside-callbacks-0.1.1.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8fc329da3c1707dd60ba2a24b8fdd4b96e0495dfa3dc1c80ea4dd8107b867c14
BLAKE2b-256 checksum
How to use checksums
35fe66d5ba944d52e1a6af93826f5c03ee4cc036ca98ef39c9468533a55eaf6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.4.9 CPython/3.10.10

Release files / pyside_callbacks-0.1.1-py3-none-any.whl

Download URL pyside_callbacks-0.1.1-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8dd5f8ce00b7d2a289675d216c70ebb0500503abff35cff3e9418bd37a56b96a
BLAKE2b-256 checksum
How to use checksums
074eb8ead624f68c1c505e7caa5a23c2e4f86b0503aeccfdc7aa7e8c8b21c252
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.4.9 CPython/3.10.10

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page