Python > Advanced Topics and Specializations > Specific Applications (Overview) > GUI Development (Tkinter, PyQt, Kivy)

PyQt5 Simple Window

This snippet demonstrates creating a simple window using PyQt5. It shows the basic structure of a PyQt application, including creating a QApplication, a QWidget, and displaying the window.

Complete Code

This code creates a simple PyQt5 window. QApplication is the main application object. QWidget is the base class for all UI elements. setGeometry sets the window's position and size. setWindowTitle sets the title bar text. show displays the window. app.exec_() starts the event loop, and sys.exit ensures a clean exit.

import sys
from PyQt5.QtWidgets import QApplication, QWidget

class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('Simple PyQt Window')
        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Concepts Behind the Snippet

The snippet demonstrates the following key PyQt concepts:

  • Application Instance: The QApplication manages the GUI application's control flow and settings.
  • Widget Hierarchy: Widgets are organized in a hierarchical structure, with parent-child relationships.
  • Event Loop: The app.exec_() function starts the event loop, which handles user interactions (e.g., mouse clicks, keyboard presses).
  • Signal and Slots: (While not explicitly shown here, PyQt uses a signal and slot mechanism for communication between objects.)

Real-Life Use Case

This snippet is the foundation for any PyQt application. It can be expanded to create:

  1. Desktop Applications: Full-featured applications with menus, toolbars, and complex UI layouts.
  2. Data Visualization Tools: Displaying charts, graphs, and other visual representations of data.
  3. Multimedia Players: Creating applications for playing audio and video files.

Best Practices

  • Object-Oriented Programming: PyQt encourages an object-oriented approach, making code modular and maintainable.
  • Signal and Slot Connections: Use signals and slots for communication between objects, as this decouples the code.
  • Resource Management: Properly manage memory and resources to avoid leaks.
  • GUI Design Principles: Follow GUI design principles for creating user-friendly interfaces.

When to Use PyQt

PyQt is suitable for:

  • Complex GUI applications.
  • Applications requiring a highly customized look and feel.
  • Cross-platform applications where native-looking UIs are desired.
  • Applications involving database interaction, network communication, or multimedia processing.

Alternatives

Alternatives to PyQt include Tkinter, Kivy, and wxPython. Tkinter is simpler and comes standard with Python, but lacks the advanced features of PyQt. Kivy is designed for multi-touch applications. wxPython aims for a native look and feel on each platform.

Pros

  • Rich set of widgets and features.
  • Highly customizable.
  • Cross-platform compatible.
  • Strong community support.

Cons

  • Steeper learning curve than Tkinter.
  • Requires installation of PyQt.
  • Commercial license may be required for certain applications.

FAQ

  • How do I add a button to the window?

    You can add a button using QPushButton. For example: button = QPushButton('Click Me', self); button.move(50, 50). You can then connect the button's clicked signal to a slot function.
  • How do I change the window's background color?

    You can change the background color using stylesheets. For example: self.setStyleSheet("background-color: lightblue;").