Pyside6学习(一)
任何的开始先从helloworld!,废话不多说,先看官方第一个hello world
import sys
import random
from PySide6 import QtCore,QtWidgets,QtGui
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.hello = ["Hallo World", "Hei maailma", "Hola Mundo", "你好,世界"]
self.button = QtWidgets.QPushButton("点击我")
self.text = QtWidgets.QLabel("Hello World",alignment = QtCore.Qt.AlignCenter)
self.layout = QtWidgets.QVBoxLayout(self)
self.layout.addWidget(self.text)
self.layout.addWidget(self.button)
self.button.clicked.connect(self.magic)
self.setWindowTitle("你好世界")
@QtCore.Slot()
def magic(self):
self.text.setText(random.choice(self.hello))
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = MyWidget()
widget.resize(800,600)
widget.show()
sys.exit(app.exec_())
运行效果如下
主要是就给按钮绑定了random.choice 随机抽取一种语言的Hello world
官方文档第一个helloworld
import sys
from PySide6.QtWidgets import QApplication,QLabel
#QApplication需要一个系统参数,sys.argv
app = QApplication([])
#label 支持类似于html的标签
label = QLabel("<font color=red size = 40>Hello World!</font>")
#显示label
label.show()
#开始程序主循环
app.exec_()
第三个hello,关于信号的
点击按钮,输出按钮被点击了,hello!(感觉程序员跟hello赶上了,一次不行两次,两次不行三次)
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("Clicked me")
button.clicked.connect(say_hello)
button.show()
app.exec_()
博观而约取,厚积而薄发。