python3.6 & pyQt5 & QtDesigner 簡易入門教程
1. python 官網下載安裝python3.6並配置好環境;
2.cmd下 運行:
pip install PyQt5 安裝PyQt庫;
3.cmd下運行:
pip3.6 install PyQt5-tools 安裝QtDesigner
QtDesigner一般默認被安裝到Python36\Lib\site-packages\pyqt5-tools路徑下。
安裝成功后在此路徑下打開designer.exe即可進行QT界面的設計。
設計完成后保存為.ui格式文件。其實.ui文件就是一個xml文件,里面有界面元素的各種信息。
然后在python代碼中使用loadUi()函數加載ui文件。這個過程其實是解析xml文件並轉譯為python程序對象的過程。
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
loadUi('qtdesigner.ui', self)
self.pushButton.clicked.connect(self.say)
def say(self):
self.label.setText("哈哈哈")
print("哈哈哈")
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
加載后界面內的各種對象都可以被python程序代碼調用,屬性也可以動態改變。對象的事件也可以綁定程序。
