文章目录
- 参考
- 说明
- 代码模块化
- `QMainWindow`和`QWidget`区别
- 通过`Label`显示图片
-
- 复选框和单选框
参考
- 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 ButtonHolder
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ButtonHolder()
window.show()
app.exec()
button_holder.py
from PySide6.QtWidgets import QMainWindow, QPushButton, QVBoxLayout
class ButtonHolder(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Button Holder")
button = QPushButton("Click Me")
button.setCheckable(True)
button.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)
def on_click1(self):
print("Button clicked")
def on_click2(self, state: bool):
print("Button clicked:", state)
def on_press(self):
print("Button pressed")
def on_release(self):
print("Button released")
def on_toggled(self, state: bool):
print("Button toggled:", state)
QMainWindow
和QWidget
区别
QMainWindow
比QWidget
多了菜单栏,工具栏,状态栏;- 在向主窗体添加元素(比如按钮)时,
QMainWindow
可以直接通过setCentralWidget()
实现;而QWidget
需要通过Layout
间接实现;
通过Label
显示图片
将UI文件编译成Python文件
- 在
Qt Designer
的Resource Browser
里面创建一个资源文件,然后在资源文件中添加需要显示的图片; - 在
Qt Designer
添加一个QLabel
,在其pixmap
属性中添加图片; - 通过
pyside6-rcc.exe -o image_rc.py .\image.qrc
将资源文件编译成py
文件;(注意命名规则:必须是xxx_rc.py
,xxx
是第一步中创建的资源文件的文件名) - 通过
pyside6-uic.exe -o ui_test.py .\test.ui
将UI
文件编译成py
文件; - 通过以下代码导入UI:
from PySide6.QtWidgets import QApplication, QWidget
from ui_test import Ui_Form
class MainWindow(QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
直接导入UI文件
- 在
Qt Designer
的Resource Browser
里面创建一个资源文件,然后在资源文件中添加需要显示的图片; - 在
Qt Designer
添加一个QLabel
,在其pixmap
属性中添加图片;
复选框和单选框
widget.py
from PySide6.QtWidgets import QWidget, QCheckBox, QRadioButton, QHBoxLayout, QVBoxLayout, QGroupBox, QButtonGroup
class Widget(QWidget):
def __init__(self):
super().__init__()
os = QGroupBox("Choose operating system")
win = QCheckBox("Windows")
win.clicked.connect(self.win_checked)
win.setChecked(True)
linux = QCheckBox("Linux")
mac = QCheckBox("Mac")
os_layout = QVBoxLayout()
os_layout.addWidget(win)
os_layout.addWidget(linux)
os_layout.addWidget(mac)
os.setLayout(os_layout)
drink = QGroupBox("Choose your favorite drink")
beer = QCheckBox("Beer")
beer.toggled.connect(self.beer_checked)
juice = QCheckBox("Juice")
coffee = QCheckBox("Coffee")
beer.setChecked(True)
exclusive_btn = QButtonGroup(self)
exclusive_btn.addButton(beer)
exclusive_btn.addButton(juice)
exclusive_btn.addButton(coffee)
exclusive_btn.setExclusive(True)
drink_layout = QVBoxLayout()
drink_layout.addWidget(beer)
drink_layout.addWidget(juice)
drink_layout.addWidget(coffee)
drink.setLayout(drink_layout)
answer = QGroupBox("Answer")
answerA = QRadioButton("A")
answerA.clicked.connect(self.answerA_checked)
answerB = QRadioButton("B")
answerC = QRadioButton("C")
answer_layout = QVBoxLayout()
answer_layout.addWidget(answerA)
answer_layout.addWidget(answerB)
answer_layout.addWidget(answerC)
answer.setLayout(answer_layout)
h_layout = QHBoxLayout()
h_layout.addWidget(os)
h_layout.addWidget(drink)
v_layout = QVBoxLayout()
v_layout.addLayout(h_layout)
v_layout.addWidget(answer)
self.setLayout(v_layout)
def win_checked(self):
if self.sender().isChecked():
print("Windows is checked")
def beer_checked(self):
if self.sender().isChecked():
print(self.sender().text() + " is checked")
def answerA_checked(self):
if self.sender().isChecked():
print(self.sender().text() + " is checked")
def exclusive_checked(self):
print(self.sender())
main.py
import sys
from PySide6.QtWidgets import QApplication
from widget import Widget
app = QApplication(sys.argv)
window = Widget()
window.show()
app.exec()