Learn Python GUI Development for Desktop – PySide6 and Qt Tutorial
说明
PySide6编程可以分为两大类:QWidget 和 QML!
代码模块化
main.py
import sys
from PySide6.QtWidgets import QApplication
from button_holder import ButtonHolderif __name__ =="__main__":app = QApplication(sys.argv)window = ButtonHolder()window.show()app.exec()
button_holder.py
from PySide6.QtWidgets import QMainWindow, QPushButton, QVBoxLayoutclassButtonHolder(QMainWindow):def__init__(self):super().__init__()self.setWindowTitle("Button Holder")button = QPushButton("Click Me")button.setCheckable(True)# must for triggering toggledbutton.clicked.connect(self.on_click1)button.clicked.connect(self.on_click2)button.pressed.connect(self.on_press)button.released.connect(self.on_release)button.toggled.connect(self.on_toggled) self.setCentralWidget(button)defon_click1(self):print("Button clicked")defon_click2(self, state:bool):# seems same with toggledprint("Button clicked:", state)defon_press(self):print("Button pressed")defon_release(self):print("Button released")defon_toggled(self, state:bool):# only triggered by checkable buttonprint("Button toggled:", state)