PyQt界面绘制及py转换调用
1、首先相关库安装:
pip install PyQt5
pip install PyQt5-tools
2、安装好后配置pycharm
File-->Setting-->Tools-->External Tools-->+新增-->edit Tool
Qt Designer中的参数配置:
Program(pyqt5_tools对应的安装文件路径):D:\python\Lib\site-packages\pyqt5_tools\Qt\bin\designer.exe
Working directory(统一设置):$ProjectFileDir$
PyUIC中的参数配置:
Program(python安装目录):D:\python\python.exe
Argument(统一设置):-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
Working directory(统一设置):$ProjectFileDir$
3、配置完毕后,在pycharm工程目录中,单击工程右键,选择External Tools 中我们设置好的Qt Designer进入(或者从Tools 中进入 External Tools )QT界面编辑就行,界面编写完毕后点击file保存:
4、对编辑保存好的UI文件进行.py转换,转换完后在工程中会生产对应的.py文件:
5、在生产的.py文件末尾加入以下代码进行运行,如果运行报错,更加相应的错误进行修改,注意
class Ui_MainWindow(object):名和if中的Ui_MainWindow要一直,如果有其他错误可能是本版本不支持相应的组件功能,注释掉就行,一般情况下不影响使用:
if __name__=="__main__":
import sys
app=QtWidgets.QApplication(sys.argv)
widget=QtWidgets.QWidget()
ui=Ui_MainWindow()
ui.setupUi(widget)
widget.show()
sys.exit(app.exec_())
UI转换出来的PY文件源码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'UI13.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# 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(1064, 782)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 271, 191))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton_Test1 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_Test1.setObjectName("pushButton_Test1")
self.verticalLayout.addWidget(self.pushButton_Test1)
self.pushButton_Test2 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_Test2.setObjectName("pushButton_Test2")
self.verticalLayout.addWidget(self.pushButton_Test2)
self.pushButton_Test3 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_Test3.setObjectName("pushButton_Test3")
self.verticalLayout.addWidget(self.pushButton_Test3)
self.pushButton_Test1.raise_()
self.pushButton_Test3.raise_()
self.pushButton_Test2.raise_()
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(10, 250, 1041, 501))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.timeEdit = QtWidgets.QTimeEdit(self.centralwidget)
self.timeEdit.setGeometry(QtCore.QRect(910, 30, 118, 22))
self.timeEdit.setObjectName("timeEdit")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(400, 20, 321, 71))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1064, 23))
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_Test1.setText(_translate("MainWindow", "Test1"))
self.pushButton_Test2.setText(_translate("MainWindow", "Test2"))
self.pushButton_Test3.setText(_translate("MainWindow", "Test3"))
self.label.setText(_translate("MainWindow", "测试"))
6、调用绘制好的UI界面并赋予相应功能
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1064, 782)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 271, 191))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton_Test1 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_Test1.setObjectName("pushButton_Test1")
self.verticalLayout.addWidget(self.pushButton_Test1)
self.pushButton_Test2 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_Test2.setObjectName("pushButton_Test2")
self.verticalLayout.addWidget(self.pushButton_Test2)
self.pushButton_Test3 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_Test3.setObjectName("pushButton_Test3")
self.verticalLayout.addWidget(self.pushButton_Test3)
self.pushButton_Test1.raise_()
self.pushButton_Test3.raise_()
self.pushButton_Test2.raise_()
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(10, 250, 1041, 501))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(12)
self.tableWidget.setRowCount(20)
self.timeEdit = QtWidgets.QTimeEdit(self.centralwidget)
self.timeEdit.setGeometry(QtCore.QRect(910, 30, 118, 22))
self.timeEdit.setObjectName("timeEdit")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(400, 20, 321, 71))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1064, 23))
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_Test1.setText(_translate("MainWindow", "Test1"))
self.pushButton_Test2.setText(_translate("MainWindow", "Test2"))
self.pushButton_Test3.setText(_translate("MainWindow", "Test3"))
self.label.setText(_translate("MainWindow", "测试"))
class MainWindow(QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton_Test1.clicked.connect(self.TestBtn1)
self.pushButton_Test2.clicked.connect(self.TestBtn2)
self.pushButton_Test3.clicked.connect(self.TestBtn3)
def TestBtn1(self):
print("Test1")
self.label.setText("Test1")
def TestBtn2(self):
self.label.setText("Test2")
print("Test2")
def TestBtn3(self):
self.label.setText("Test3")
print("Test3")
if __name__=="__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
上一篇: 多线程编程(NSThread、NSOperation、GCD)
下一篇: ToolBar使用
推荐阅读