Python Qt Designer安装过程和转换ui文件
用Qt Designer来设计GUI界面挺直观的,这篇简单介绍Qt Designer的安装和ui文件的转换。
Qt Designer的安装
1. 通过pip安装pyqt5,pyqt5_tools
c:\xxx\python> pip install pyqt5
c:\xxx\python> pip install pyqt5_tools
2. 设置Path={Python安装路径}\Lib\site-packages\pyqt5_tools
然后,打开qt5-application下的designer.exe,路径:{Python安装路径}\Lib\site-packages\qt5_applications\Qt\bin
打开后的界面:
将界面保存文件为source.ui
使用pyuic5转换 source.ui
pyuic5在安装python后就可以在命令行里使用了,使用方式如下:
c:\xxx\python> pyuic5 -o destination.py source.ui
初始内容及转换后的内容如下:
可以加入自己的python代码(加粗部分),也可以将自己的代码合并成一个类:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'source.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 10, 771, 501))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(10, 520, 231, 31))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(250, 520, 231, 31))
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
def main():
app = QtWidgets.QApplication(sys.argv)
main_window = QtWidgets.QMainWindow()
form = Ui_MainWindow()
form.setupUi(main_window)
form.retranslateUi(main_window)
main_window.show()
sys.exit(app.exec_())
if __name__ =='__main__':
main()