python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > 使用QT Designer创建基本窗口

PyQT5之使用QT Designer创建基本窗口方式

作者:Trivis Kylee

这篇文章主要介绍了PyQT5之使用QT Designer创建基本窗口方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

环境配置

使用QT Designer布局窗体

UI文件的编译

将.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_())

编译后的.PY源码

# -*- 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_())

运行结果

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文