Building Blocks of PySide6: A Deep Dive into Qt Modules

 


PySide6 is a powerful framework for creating graphical user interfaces (GUIs) in Python. And its main modules are QtCore, QtGui, and QtWidgets.

These modules are essential parts of the Qt framework, upon which PySide6 is built.

Each module serves a distinct purpose:

  1. QtCore: This module contains core non-GUI functionality. It includes classes for event handling, file and data handling, time, threads, and more. It's the foundation on which the other modules build.
  2. QtGui: As the name suggests, this module focuses on graphical user interface functionality. It provides classes for handling graphical elements such as fonts, colors, images, and other graphical primitives.
  3. QtWidgets: This module contains a vast collection of UI elements (widgets) that you can use to create your GUI applications. It includes classes for buttons, layouts, dialogs, input fields, and various other components that you see and interact with in a typical GUI.
+----------------+
|     QtCore     |   <--- Core functionality
+----------------+
|     QtGui      |   <--- Graphical elements
+----------------+
|   QtWidgets    |   <--- User interface widgets
+----------------+
import sys
from PySide6.QtCore import QTimer, QDateTime
from PySide6.QtGui import QFont
from PySide6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout

class TimerApp(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Timer App")
        self.setMinimumSize(400, 400)

        layout = QVBoxLayout()

        self.time_label = QLabel("Current Time: ", self)
        font = QFont("Arial", 16)
        self.time_label.setFont(font)

        layout.addWidget(self.time_label)

        self.setLayout(layout)

        # Create a timer
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_time)
        self.timer.start(1000)

    def update_time(self):
        current_time = QDateTime.currentDateTime().toString("hh:mm:ss")
        self.time_label.setText(f"Current Time: {current_time}")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    timer_app = TimerApp()
    timer_app.show()
    sys.exit(app.exec())


Post a Comment

Previous Post Next Post