赞
踩
- import sys
- from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
-
- app = QApplication(sys.argv)
-
- # 创建一个窗口部件
- widget = QWidget()
-
- # 创建一个垂直布局管理器
- layout = QVBoxLayout()
-
- # 创建两个按钮
- button1 = QPushButton("Button 1")
- button2 = QPushButton("Button 2")
-
- # 将按钮添加到布局中
- layout.addWidget(button1)
- layout.addWidget(button2)
-
- # 将布局设置为窗口部件的布局
- widget.setLayout(layout)
-
- # 显示窗口部件
- widget.show()
-
- sys.exit(app.exec())
- import sys
- from PySide6.QtWidgets import QApplication, QLabel
-
- app = QApplication(sys.argv)
- label = QLabel("<font color=red size=40>Hello World!</font>")
- label.show()
- app.exec()
- import sys
- from PySide6.QtWidgets import QApplication, QPushButton
- from PySide6.QtCore import Slot
-
- @Slot()
- def say_hello():
- print("Button clicked, Hello!") #输出到终端
-
- # Create the Qt Application
- app = QApplication(sys.argv)
-
- # Create a button, connect it and show it 创建、连接 和 展示
- button = QPushButton("Click me")
- button.clicked.connect(say_hello)
- button.show()
-
- # Run the main Qt loop
- app.exec()
- import sys
- from PySide6.QtWidgets import QApplication, QPushButton
-
- def function():
- print("The 'function' has been called!")
-
- app = QApplication()
-
- button = QPushButton("Call function")
- button.clicked.connect(function) # click 单击 是信号, function 是槽 要执行的操作;
- # connect 意为 将信号和槽 连接起来
- button.show()
-
- sys.exit(app.exec())
2. 信号类型
- signal1 = Signal(int) # Python types
- signal2 = Signal(QUrl) # Qt Types
- signal3 = Signal(int, str, int) # more than one type
- signal4 = Signal((float,), (QDate,)) # optional types
- import sys
- from PySide6.QtWidgets import QApplication, QPushButton
- from PySide6.QtCore import QObject, Signal, Slot
-
-
- class Communicate(QObject):
- # create two new signals on the fly: one will handle
- # int type, the other will handle strings
- speak = Signal((int,), (str,))
-
- def __init__(self, parent=None):
- super().__init__(parent)
-
- self.speak[int].connect(self.say_something)
- # speak[int] 信号;connect 连接;say_something方法 槽,就是有信号后,要执行的操作
- self.speak[str].connect(self.say_something)
-
- # define a new slot that receives a C 'int' or a 'str'
- # and has 'say_something' as its name
- @Slot(int)
- @Slot(str)
- def say_something(self, arg):
- if isinstance(arg, int):
- print("This is a number:", arg)
- elif isinstance(arg, str):
- print("This is a string:", arg)
-
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- someone = Communicate()
-
- # emit 'speak' signal with different arguments.
- # we have to specify the str as int is the default
- someone.speak.emit(10)
- someone.speak[str].emit("Hello everybody!")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。