您好,登錄后才能下訂單哦!
這篇文章主要介紹“PyQT5中怎么使用QT Designer創建基本窗口方式”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“PyQT5中怎么使用QT Designer創建基本窗口方式”文章能幫助大家解決問題。
pip install pyqt5 (安裝pyQT5庫):pyuic5包含在內
pip install pyqt5-tools (安裝QT-Designer)
將.UI文件編譯為.PY文件(CMD命令行中):
pyuic5 xxx.ui -o xxx.py #不可以存在中文!
打開編譯后的.py文件,新建SLOT()函數并修改連接函數:
在末尾添加運行代碼:
if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'C:\Users\Geclipse\Desktop\untitled.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) MainWindow.setMouseTracking(False) MainWindow.setWindowOpacity(1.0) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(300, 0, 161, 71)) font = QtGui.QFont() font.setPointSize(12) font.setBold(True) font.setItalic(False) font.setUnderline(False) font.setWeight(75) font.setStrikeOut(False) self.label.setFont(font) self.label.setObjectName("label") self.textEdit = QtWidgets.QTextEdit(self.centralwidget) self.textEdit.setGeometry(QtCore.QRect(20, 220, 751, 321)) self.textEdit.setObjectName("textEdit") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(300, 90, 141, 61)) font = QtGui.QFont() font.setPointSize(12) font.setBold(True) font.setWeight(75) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26)) self.menubar.setObjectName("menubar") self.menu_1 = QtWidgets.QMenu(self.menubar) self.menu_1.setObjectName("menu_1") self.menu_2 = QtWidgets.QMenu(self.menubar) self.menu_2.setObjectName("menu_2") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.actionFunction1 = QtWidgets.QAction(MainWindow) self.actionFunction1.setObjectName("actionFunction1") self.actionFunction2 = QtWidgets.QAction(MainWindow) self.actionFunction2.setObjectName("actionFunction2") self.actionExit = QtWidgets.QAction(MainWindow) self.actionExit.setObjectName("actionExit") self.actionExit_2 = QtWidgets.QAction(MainWindow) self.actionExit_2.setObjectName("actionExit_2") self.menu_1.addAction(self.actionFunction1) self.menu_1.addAction(self.actionFunction2) self.menu_1.addSeparator() self.menu_1.addAction(self.actionExit_2) self.menubar.addAction(self.menu_1.menuAction()) self.menubar.addAction(self.menu_2.menuAction()) self.textEdit.setText("pyuic untitled.ui -o untitled.py") self.retranslateUi(MainWindow) self.pushButton.clicked.connect(self.add_hello_qt) QtCore.QMetaObject.connectSlotsByName(MainWindow) def add_hello_qt(self): self.textEdit.setText("Hello QT!") #★★★ def retranslateUi(eslf,MainWindow) #translate翻譯 #retranslate重譯(對界面中控件所進行的其他調整:如設置標題會出現在這里) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "This is a simple title")) self.label.setText(_translate("MainWindow", "Simple Widget")) self.pushButton.setStatusTip(_translate("MainWindow", "click this will add ‘hello QT' in below area")) self.pushButton.setText(_translate("MainWindow", "Hello QT")) self.menu_1.setTitle(_translate("MainWindow", "菜單欄1")) self.menu_2.setTitle(_translate("MainWindow", "菜單欄2")) self.actionFunction1.setText(_translate("MainWindow", "Function1")) self.actionFunction2.setText(_translate("MainWindow", "Function2")) self.actionExit.setText(_translate("MainWindow", "Exit")) self.actionExit_2.setText(_translate("MainWindow", "Exit")) #下面的這些部分需要手動打開py文件進行添加 if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
關于“PyQT5中怎么使用QT Designer創建基本窗口方式”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。