1、安装PyQt5:pip install PyQt5,在搜索框中,出现图片所示,即为安装成功:
2、安装Qt Designer:pip install pyqt5-tools,在cmd中输入pyuic5,返回“Error:one input ui-file must be specified”,说明安装成功。
3、在Qt Designer中绘制GUI,并保存为test.ui
4、在保存文件夹中,用powershell运行:pyuic5 - o test.py test.ui,生成test.py文件;
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# 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
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(459, 331)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lb_Show1 = QtWidgets.QLabel(self.centralwidget)
self.lb_Show1.setGeometry(QtCore.QRect(90, 90, 101, 61))
self.lb_Show1.setObjectName("lb_Show1")
self.btn_Show1 = QtWidgets.QPushButton(self.centralwidget)
self.btn_Show1.setGeometry(QtCore.QRect(260, 110, 56, 17))
self.btn_Show1.setObjectName("btn_Show1")
self.btn_Show2 = QtWidgets.QPushButton(self.centralwidget)
self.btn_Show2.setGeometry(QtCore.QRect(260, 170, 56, 17))
self.btn_Show2.setObjectName("btn_Show2")
self.lb_Show2 = QtWidgets.QLabel(self.centralwidget)
self.lb_Show2.setGeometry(QtCore.QRect(90, 180, 41, 31))
self.lb_Show2.setObjectName("lb_Show2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 459, 18))
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.lb_Show1.setText(_translate("MainWindow", "TextLabel"))
self.btn_Show1.setText(_translate("MainWindow", "Show"))
self.btn_Show2.setText(_translate("MainWindow", "Show"))
self.lb_Show2.setText(_translate("MainWindow", "TextLabel"))
5、在文件夹中,新建文件main.py
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
import test
//定义槽函数
def function_1():
ui.lb_Show1.setText("abc")
def function_2():
ui.lb_Show2.setText("aabbcc")
//初始化GUI
def Initial_GUI():
//连接信号槽函数
ui.btn_Show1.clicked.connect(function_1)
ui.btn_Show2.clicked.connect(function_2)
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = test.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
Initial_GUI()
sys.exit(app.exec_())
第二种:用loadUi来加载ui文件,代码如下:
import os
import sys
from PyQt5 import QtGui,QtWidgets,QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.uic import loadUi
import cv2
class my_software(QMainWindow):
def __init__(self,parent = None):
super(my_software,self).__init__(parent)
loadUi('UI_Test.ui',self)
self.Initial_GUI()
def Initial_GUI(self):
self.setWindowTitle("ImageShow")
self.lb_Screen.setFixedSize(self.lb_Screen.width(),self.lb_Screen.height())
self.btn_Open.clicked.connect(self.Btn_Open)
self.btn_Process.clicked.connect(self.Btn_Process)
#窗口关闭时间
def Btn_Close(self,event):
event.accept()
def Btn_Open(self):
self.img_name,self.img_type = QFileDialog.getOpenFileName(self,"打开图片","","*.jpg;;*.png;;All Files(*)")
self.img_in = cv2.imread(self.img_name)
self.Display_Image(self.img_in)
def Btn_Process(self):
self.img_Process = cv2.putText(self.img_in,"Hello",(20,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,255),2)
self.Display_Image(self.img_Process)
def Display_Image(self,image):
if(len(image.shape) == 3):
image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
Q_img = QImage(image.data,
image.shape[1],
image.shape[0],
QImage.Format_RGB888)
elif(len(image.shape) == 1):
Q_img = QImage(image.data,
image.shape[1],
image.shape[0],
QImage.Format_Indexed8)
else:
Q_img = QImage(image.data,
image.shape[1],
image.shape[0],
QImage.Format_RGB888)
# self.img_scale = QtGui.QPixmap(self.img_in).scaled(self.lb_Screen.width(),self.lb_Screen.height())
# self.lb_Screen.setPixmap(self.img_scale)
self.lb_Screen.setPixmap(QtGui.QPixmap(Q_img))
self.lb_Screen.setScaledContents(True)
if __name__ == "__main__":
app = QApplication(sys.argv)
my = my_software()
my.show()
sys.exit(app.exec_())