PyQt在windows10系统下编写软件

12 篇文章 0 订阅

1、安装PyQt5:pip install PyQt5,在搜索框中,出现图片所示,即为安装成功:

designer_install

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值