这篇文章主要介绍“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。