Getting started with Pyqt5--design a calculation screen PPI applet with qtdesigner (qtdesigner/pyuic/pyinstaller/python)

This article uses the pyqt5 package in python to design an interface for calculating the PPI applet, and then uses pyuic to convert the ui file of the interface into a py file. Create a new py file to inherit the class in the interface py file, and define the function corresponding to each button. After completion, use pyinstaller to package it into an exe program and run it directly. Next, the implementation process of the whole program will be introduced.

  • Pre-installation preparations (this article uses pycharm with python3.7 to complete)
  • It is recommended to use pip install to install, and add designer and pyuic to external tools after installation
  • pyqt5
  • PyQt5-tools (there is only designer.exe in this)
  • pyinstaller

1. Use qtdesigner to design an interface

  • step
  • 1.pycharm->tools->external tools->qtdesigner
  • 2. Create a new Dialog without Buttons (you can also choose main window and widget, the suggestion here is consistent with this article) (supplement: the difference between the three windows )
  • 3. Design the program interface according to the picture
  • 4. Save as .ui file
    insert image description here

2. Use pyuic to convert ui files to py files

  • step
  • 1. Open the saved ui file in pycharm
  • 2.点tools->external tools->pyuic
  • 3. The generated py file is as follows
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ppi.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_ppi(object):
    def setupUi(self, ppi):
        ppi.setObjectName("ppi")
        ppi.resize(458, 328)
        self.label = QtWidgets.QLabel(ppi)
        self.label.setGeometry(QtCore.QRect(120, 30, 211, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(18)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.groupBox = QtWidgets.QGroupBox(ppi)
        self.groupBox.setGeometry(QtCore.QRect(80, 80, 281, 171))
        self.groupBox.setObjectName("groupBox")
        self.label_2 = QtWidgets.QLabel(self.groupBox)
        self.label_2.setGeometry(QtCore.QRect(30, 40, 54, 12))
        self.label_2.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label_2.setAutoFillBackground(False)
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.groupBox)
        self.label_3.setGeometry(QtCore.QRect(30, 70, 54, 12))
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(self.groupBox)
        self.label_4.setGeometry(QtCore.QRect(30, 100, 54, 12))
        self.label_4.setAlignment(QtCore.Qt.AlignCenter)
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(self.groupBox)
        self.label_5.setGeometry(QtCore.QRect(30, 140, 54, 12))
        self.label_5.setAlignment(QtCore.Qt.AlignCenter)
        self.label_5.setObjectName("label_5")
        self.comboBox = QtWidgets.QComboBox(self.groupBox)
        self.comboBox.setGeometry(QtCore.QRect(100, 40, 151, 22))
        self.comboBox.setAcceptDrops(False)
        self.comboBox.setAutoFillBackground(True)
        self.comboBox.setEditable(True)
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox_2 = QtWidgets.QComboBox(self.groupBox)
        self.comboBox_2.setGeometry(QtCore.QRect(100, 70, 151, 22))
        self.comboBox_2.setEditable(True)
        self.comboBox_2.setObjectName("comboBox_2")
        self.comboBox_2.addItem("")
        self.comboBox_2.addItem("")
        self.comboBox_3 = QtWidgets.QComboBox(self.groupBox)
        self.comboBox_3.setGeometry(QtCore.QRect(100, 100, 151, 22))
        self.comboBox_3.setEditable(True)
        self.comboBox_3.setObjectName("comboBox_3")
        self.comboBox_3.addItem("")
        self.comboBox_3.addItem("")
        self.comboBox_3.addItem("")
        self.comboBox_3.addItem("")
        self.comboBox_3.addItem("")
        self.comboBox_3.addItem("")
        self.result = QtWidgets.QTextEdit(self.groupBox)
        self.result.setGeometry(QtCore.QRect(100, 130, 151, 31))
        self.result.setObjectName("result")
        self.enter = QtWidgets.QPushButton(ppi)
        self.enter.setGeometry(QtCore.QRect(150, 260, 75, 23))
        self.enter.setObjectName("enter")
        self.clear = QtWidgets.QPushButton(ppi)
        self.clear.setGeometry(QtCore.QRect(240, 260, 75, 23))
        self.clear.setObjectName("clear")

        self.retranslateUi(ppi)
        QtCore.QMetaObject.connectSlotsByName(ppi)

    def retranslateUi(self, ppi):
        _translate = QtCore.QCoreApplication.translate
        ppi.setWindowTitle(_translate("ppi", "Dialog"))
        self.label.setText(_translate("ppi", "计算屏幕PPI小程序"))
        self.groupBox.setTitle(_translate("ppi", "请输入"))
        self.label_2.setText(_translate("ppi", "长"))
        self.label_3.setText(_translate("ppi", "宽"))
        self.label_4.setText(_translate("ppi", " 英寸"))
        self.label_5.setText(_translate("ppi", "PPI"))
        self.comboBox.setItemText(0, _translate("ppi", "2560"))
        self.comboBox.setItemText(1, _translate("ppi", "1920"))
        self.comboBox_2.setItemText(0, _translate("ppi", "1440"))
        self.comboBox_2.setItemText(1, _translate("ppi", "1080"))
        self.comboBox_3.setItemText(0, _translate("ppi", "27"))
        self.comboBox_3.setItemText(1, _translate("ppi", "24"))
        self.comboBox_3.setItemText(2, _translate("ppi", "23.8"))
        self.comboBox_3.setItemText(3, _translate("ppi", "16"))
        self.comboBox_3.setItemText(4, _translate("ppi", "14"))
        self.comboBox_3.setItemText(5, _translate("ppi", "13.8"))
        self.enter.setText(_translate("ppi", "确定"))
        self.clear.setText(_translate("ppi", "清除"))

3. Create a new ppi_new.py

Note: If there is no specific reason for the error when debugging: run->edit configurations->emulate terminal in output console

# -*- coding: utf-8 -*-
# 建议用from...import 可以减小程序大小(实际上还是有30多M,还没找到更好的办法)
from sys import argv,exit
# from PyQt5.QtWidgets import *
# from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow,QApplication
from ppi import Ui_ppi

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent) # 继承界面文件的属性,方便调用和修改,因为自动生成的界面py中有一句:WARNING! All changes made in this file will be lost!
        self.ui = Ui_ppi() # 调用界面类
        self.ui.setupUi(self) # 调用界面函数

        self.ui.clear.clicked.connect(self.clear) # 调用自定义的清除按钮
        self.ui.enter.clicked.connect(self.enter) # 调用自定义的确定按钮

    def enter(self):
        length = self.ui.comboBox.currentText() # 获取复选框中的内容
        width = self.ui.comboBox_2.currentText()
        inch = self.ui.comboBox_3.currentText()
        msg1 = self.calculate(length=length,width=width,inch=inch) # 计算结果
        ppi_result = str(round(msg1,2)) # 将数字转换为文本,并取2位小数,否则会报错格式不对应
        self.ui.result.setText(ppi_result) # 将结果发送到文本框中

    def calculate(self, length, width, inch):
        ppi_result = ((float(length)**2 + float(width)**2)**(1/2))/float(inch) # 定义函数,注意这里要加float否则会报错格式不对应
        return ppi_result

    def clear(self):
        self.ui.result.clear() # 定义清除按钮


if __name__ == '__main__':
    app = QApplication(argv) # 初始化窗口
    win = MainWindow()
    win.show() # 运行程序
    exit(app.exec_()) # 退出窗口

4. Use pyinstaller to package into exe program

If the program interface can appear in run in pycharm, and the function is executed normally, it means that it can be used normally, and then it can be packaged with pyinstaller

  • step
  • win+r->cmd->enter cd /de:\qtdesigner\ppi
  • (Note: enter the directory where the ppi_new file is located e:\qtdesigner\ppi\ppi_new.py) (here you need to modify it to your own folder)
  • Enter pyinstaller -F ppi_new.py -w
  • (Note -F is to generate a single file exe, -w is not to display the command line window when running)
  • The generated exe file is in e:\qtdesigner\ppi\dist\ppi_new.exe
  • Double click the exe file to run
    operation result

Guess you like

Origin blog.csdn.net/lzykevin/article/details/103547281