Git Product home page Git Product logo

examples's Introduction

PyQt examples 2022

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.

PyQt QLabel PyQt widgets screenshot QVBoxLayout PyQt6 PyQt Signals and Slots Qt Designer Python
Hello World! Common PyQt Widgets Layouts Signals and Slots Qt Designer & Python
QML Python example Qt Text Editor PyQt6 exe Qt dark theme
QML Python example Qt Text Editor Packaging & deployment Qt Dark Theme
QPainter Python example PyQt Thread example QTreeView example in Python PyQt6 QListView
Action Shooter Chat Client Tree Views Lists
QAbstractTableModel example QAbstractTableModel example
Custom Tables PyQt database example

These examples are taken from the following book:

PyQt6 book
Python and Qt: The Best Parts
by Michael Herrmann

Official PyQt demos

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

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:

python3 -m venv venv

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:

cd "src/01 PyQt QLabel"

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

python main.py

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

Using PySide

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.

Licensing

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 official PyQt demos in src/pyqt-official are licensed under the GPL.

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.

examples's People

Contributors

aaronpinto avatar arudarin avatar day0dreamer avatar hsiafan avatar matt-meyers avatar mherrmann avatar mottosso avatar olli-suoniemi avatar rafale25 avatar sammatzko avatar snoyes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

examples's Issues

.

.

09 Qt dark theme does not work on pyqt6

At least on my linux PyQt6 it just does not run.
instead of QPalette need QPalette.ColorRole
qt.white does not work I settled on QColorConstants.White
QColorConstants.Red etc.
I added QColorConstants, QAction, QKeySequence to the
from PyQt6.QtGui import

Example for QML and fbs is missing

I'm coming from the Book "Python and Qt - The Best Parts". I don't know if this is the right spot for this issue, but it is mainly about the book.

This book uses the examples in this repo. There's one example how to use QML with PyQt. Then later on fbs is being added to the stack. Now I wanted to combine both but fell on my nose doing so.

If you use QML like in the example and add fbs to it, the frozen app won't do anything. It starts a UI process but doesn't open a window. In that situation you have to kill that process, or you can't freeze the project again, because the .exe is still running in the background. By fiddling around I found what the problem is:

The frozen app has no copy of the .qml file. So, it can't load it. You need to move it into the src/main/resources/base folder and load it like so:

engine = QQmlApplicationEngine()
engine.load(app_context.get_resource('window.qml'))

Freezing the project like this makes the frozen executable run as expected.

Yes, you can combine the information from the book to come to this solution. But I think it should be mentioned explicitly that you have to use .get_resource() for .qml files, too.

Another example of PyQt usage?

I made a video course for Packt on Reactive programming with Python and it included a bunch of code for using PyQt5 as well:

The example is a bit bigger than just showcasing one widget, it would be nice to include a link to https://github.com/rudolfolah/Reactive-Programming-in-Python/blob/master/Section%206/client/

Alternatively, I could create a PR that simplifies that example further but still gives devs an opportunity to see an example of using multiple PyQt widgets with multiple windows/forms/layouts.

QML example not working

Followed the instructions, the first example works. Trying the 06 QML Python example returns an error.

(venv) C:\Users\X\Documents\Python Scripts\qtexamples\src\01 PyQt QLabel>cd "C:\Users\User\Documents\Python Scripts\qtexamples\src\06 QML Python example"

(venv) C:\Users\X\Documents\Python Scripts\qtexamples\src\06 QML Python example>python main.py
QQmlApplicationEngine failed to load component
file:///C:/Users/X/Documents/Python Scripts/qtexamples/src/06 QML Python example/main.qml:1 plugin cannot be loaded for module "QtQuick": Cannot load library C:\Users\X\Documents\Python Scripts\qtexamples\venv\lib\site-packages\PyQt5\Qt\qml\QtQuick.2\qtquick2plugin.dll: The specified procedure could not be found.

A question on pyqt5

I am making a software and gonna distribute it (for free),so still I need to buy a licence from riverbank?

How to debug QtDesigner plugin?

In this demo :

image

If I raise Exception or print , I can't get anything output, and Custom Plugin not show in QtDesigner :

image

So , How do I debug My plugin?

07 Qt Text Editor - Errors on QAction and QKeySequence

In Pyqt6 QAction was moved to QtGui, QKeySequence requires the StandardKey addition

from PyQt6.QtWidgets import *
from PyQt6.QtGui import QKeySequence, QAction

class MainWindow(QMainWindow):
    def closeEvent(self, e):
        if not text.document().isModified():
            return
        answer = QMessageBox.question(
            window, None,
            "You have unsaved changes. Save before closing?",
            QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel
        )
        if answer & QMessageBox.Save:
            save()
            if text.document().isModified():
            	# This happens when the user closes the Save As... dialog.
            	# We do not want to close the window in this case because it
            	# would throw away unsaved changes.
                e.ignore()
                
        elif answer & QMessageBox.Cancel:
            e.ignore()

app = QApplication([])
app.setApplicationName("Text Editor")
text = QPlainTextEdit()
window = MainWindow()
window.setCentralWidget(text)

file_path = None

menu = window.menuBar().addMenu("&File")
open_action = QAction("&Open")
def open_file():
    global file_path
    path = QFileDialog.getOpenFileName(window, "Open")[0]
    if path:
        text.setPlainText(open(path).read())
        file_path = path
open_action.triggered.connect(open_file)
open_action.setShortcut(QKeySequence.StandardKey.Open)
menu.addAction(open_action)

save_action = QAction("&Save")
def save():
    if file_path is None:
        save_as()
    else:
        with open(file_path, "w") as f:
            f.write(text.toPlainText())
        text.document().setModified(False)
save_action.triggered.connect(save)
save_action.setShortcut(QKeySequence.StandardKey.Save)
menu.addAction(save_action)

save_as_action = QAction("Save &As...")
def save_as():
    global file_path
    path = QFileDialog.getSaveFileName(window, "Save As")[0]
    if path:
        file_path = path
        save()
save_as_action.triggered.connect(save_as)
menu.addAction(save_as_action)

close = QAction("&Close")
close.triggered.connect(window.close)
menu.addAction(close)

help_menu = window.menuBar().addMenu("&Help")
about_action = QAction("&About")
help_menu.addAction(about_action)
def show_about_dialog():
    text = "<center>" \
           "<h1>Text Editor</h1>" \
           "&#8291;" \
           "<img src=icon.svg>" \
           "</center>" \
           "<p>Version 31.4.159.265358<br/>" \
           "Copyright &copy; Company Inc.</p>"
    QMessageBox.about(window, "About Text Editor", text)
about_action.triggered.connect(show_about_dialog)

window.show()
app.exec()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.