10.29 qt for python3 初體驗

簡介

qt是linux下面最通用的gui 框架,可以用來開發各種GUI程序,Python, 宇宙第一動態語言,就沒有幹不了的活。他們相結合,會碰撞出什麼樣的火花呢? 我們來試試。

Python2 生命即將結束,就不再用Python2 來做教程了,推薦大家現在立刻馬上開始學python3

開始

mkdir ~/tmp/demoqt
cd ~/tmp/demoqt
pipenv --three
pipenv install PySide2
vi main.py
# 下面是代碼內容
import sys
import random
from PySide2.QtWidgets import (QApplication, QLabel, QPushButton,
QVBoxLayout, QWidget)
from PySide2.QtCore import Slot, Qt
class MyWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.hello = ["Hallo Welt", "你好,世界", "Hei maailma",
"Hola Mundo", "Привет мир"]
self.button = QPushButton("Click me!")
self.text = QLabel("Hello World")
self.text.setAlignment(Qt.AlignCenter)
self.layout = QVBoxLayout()
self.layout.addWidget(self.text)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
# Connecting the signal
self.button.clicked.connect(self.magic)
@Slot()
def magic(self):
self.text.setText(random.choice(self.hello))
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = MyWidget()
widget.resize(800, 600)
widget.show()
sys.exit(app.exec_())

# 代碼輸入完了, 執行
pipenv shell
cd ~/tmp/demoqt
python main.py

你會看到一個GUI窗口

qt for python3 初體驗


分享到:


相關文章: