任何的開始先從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_()