上一篇【轉】[PYQT | WINDOWS]使用PYQT開發PYTHON GUI視窗程式中,介紹了製造的方式,這邊我們繼續解說關於UI連結程式方法,以下會寫一個簡單的button click() 範例,透過點擊Button來置換中間Label的文字。
打開CMD 到ui檔的位置後 執行 pyuic4 your_qt_ui.ui –o output_name.py 指令。
轉換出 helloworld_ui.py
再來就直接上碼了。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 |
# -*- coding: cp950 -*-
import helloworld_ui import sys from helloworld_ui import Ui_MainWindow from PyQt4.QtGui import QMainWindow from PyQt4 import QtCore, QtGui class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) ”’ UI連結function old API QtCore.QObject.connect(self.連結的UI名子,QtCore.SIGNAL(“連動動作”),self.連動的fun名子) new API self.連結的UI名子.clicked.connect(連動的fun名字) ”’ #old API #QtCore.QObject.connect(self.Button1_name, QtCore.SIGNAL(“clicked()”), self.label_change1) #new API self.Button1_name.clicked.connect(self.label_change1) self.Button2_name.clicked.connect(self.label_change2) def label_change1(self): self.label_name.setText(‘hello world’) def label_change2(self): self.label_name.setText(‘good job’) if __name__ == ‘__main__’: app = helloworld_ui.QtGui.QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) |
結果:
【範例下載】PYQT_helloworld_20150809
1 thought on “[PYQT]使用PYQT開發PYTHON GUI視窗程式 – UI連結程式”