记录学习过程+代码
1搭建环境
安装pyqt5
在线安装
pip install pyqt5
离线下载+安装
pip download pyqt5 -d save_pathpip install
安装QtDesigner
下载地址:Qt Designer Download for Windows and Mac
ui转python
pyuic5 -x mydesign.ui -o mydesign.py
可以将QtDesigner ,pyuic5加入到pycharm,详见Python - PyQt6、QDesigner、pyuic5-tool 安装使用_sipbuild.pyproject.pyprojectoptionexception-CSDN博客
2.使用
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file '0one.ui'
#
# Created by: PyQt5 UI code generator 5.15.11
#
# 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
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtWidgets import QMainWindow, QApplicationclass Ui_Form(object):def setupUi(self, Form):Form.setObjectName("Form")Form.resize(400, 300)self.pushButton = QtWidgets.QPushButton(Form)self.pushButton.setGeometry(QtCore.QRect(100, 200, 75, 23))self.pushButton.setObjectName("pushButton")self.retranslateUi(Form)QtCore.QMetaObject.connectSlotsByName(Form)def retranslateUi(self, Form):_translate = QtCore.QCoreApplication.translateForm.setWindowTitle(_translate("Form", "Form"))self.pushButton.setText(_translate("Form", "PushButton"))import sys
class MainWindow(QMainWindow):def __init__(self):super(MainWindow,self).__init__()self.ui=Ui_Form()self.ui.setupUi(self)
app=QApplication(sys.argv)
window=MainWindow()
window.show()
sys.exit(app.exec())
案例
1简单计算器
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'vref_qt.ui'
#
# Created by: PyQt5 UI code generator 5.15.11
#
# 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
from PyQt5.QtWidgets import QMainWindow, QApplication
class Ui_Form(object):def setupUi(self, Form):Form.setObjectName("Form")Form.resize(284, 300)self.btn_cal_h_l_s = QtWidgets.QPushButton(Form)self.btn_cal_h_l_s.setGeometry(QtCore.QRect(100, 260, 75, 23))self.btn_cal_h_l_s.setObjectName("btn_cal_h_l_s")self.btn_cal_h_l_s.clicked.connect(self.cal_h_l_s)self.textBrowser = QtWidgets.QTextBrowser(Form)font = QtGui.QFont()font.setPointSize(12)self.textBrowser.setFont(font)self.textBrowser.setGeometry(QtCore.QRect(50, 10, 191, 151))self.textBrowser.setObjectName("textBrowser")self.widget = QtWidgets.QWidget(Form)self.widget.setGeometry(QtCore.QRect(60, 170, 163, 80))self.widget.setObjectName("widget")self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)self.verticalLayout.setContentsMargins(0, 0, 0, 0)self.verticalLayout.setSpacing(0)self.verticalLayout.setObjectName("verticalLayout")self.horizontalLayout = QtWidgets.QHBoxLayout()self.horizontalLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)self.horizontalLayout.setSpacing(0)self.horizontalLayout.setObjectName("horizontalLayout")self.label = QtWidgets.QLabel(self.widget)font = QtGui.QFont()font.setPointSize(12)self.label.setFont(font)self.label.setObjectName("label")self.horizontalLayout.addWidget(self.label)self.reg_02 = QtWidgets.QLineEdit(self.widget)self.reg_02.setObjectName("reg_02")self.reg_02.returnPressed.connect(self.reg_02_returnPressed)self.horizontalLayout.addWidget(self.reg_02)self.verticalLayout.addLayout(self.horizontalLayout)self.horizontalLayout_2 = QtWidgets.QHBoxLayout()self.horizontalLayout_2.setSpacing(0)self.horizontalLayout_2.setObjectName("horizontalLayout_2")self.label_2 = QtWidgets.QLabel(self.widget)font = QtGui.QFont()font.setPointSize(12)self.label_2.setFont(font)self.label_2.setObjectName("label_2")self.horizontalLayout_2.addWidget(self.label_2)self.reg_03 = QtWidgets.QLineEdit(self.widget)self.reg_03.setObjectName("reg_3")self.reg_03.returnPressed.connect(self.reg_03_returnPressed)self.horizontalLayout_2.addWidget(self.reg_03)self.verticalLayout.addLayout(self.horizontalLayout_2)self.horizontalLayout_3 = QtWidgets.QHBoxLayout()self.horizontalLayout_3.setSpacing(0)self.horizontalLayout_3.setObjectName("horizontalLayout_3")self.label_3 = QtWidgets.QLabel(self.widget)font = QtGui.QFont()font.setPointSize(12)self.label_3.setFont(font)self.label_3.setObjectName("label_3")self.horizontalLayout_3.addWidget(self.label_3)self.reg_04 = QtWidgets.QLineEdit(self.widget)self.reg_04.setObjectName("reg_04")self.reg_04.returnPressed.connect(self.cal_h_l_s)self.horizontalLayout_3.addWidget(self.reg_04)self.verticalLayout.addLayout(self.horizontalLayout_3)self.retranslateUi(Form)QtCore.QMetaObject.connectSlotsByName(Form)def h_l_s(self,reg_2, reg_3, reg_4):# print(reg_2,reg_3,reg_4)h = reg_2 >> 3l = reg_3 >> 4s = reg_4 >> 3l_s = l + sreg_2 = (reg_2 & 0x07) | (l_s << 3)txt=''if l_s > 31:txt='l + s>31'# returnelse:# print("afereg[0x02] = ", hex(reg_2), ';', sep='')# print("afereg[0x03] = ", hex(reg_3), ';', sep='')# print("afereg[0x04] = ", hex(reg_4), ';', sep='')# print()txt=f'x = {hex(reg_2)};\ny = {hex(reg_3)};\nz = {hex(reg_4)};\n'self.textBrowser.setText(txt)def cal_h_l_s(self):# 获取输入框中的内容reg_2_val = self.reg_02.text()reg_3_val = self.reg_03.text()reg_4_val = self.reg_04.text()# print("afereg[0x02] =", reg_2_val)# print("afereg[0x02] =", reg_3_val)# print("afereg[0x02] =", reg_4_val)self.h_l_s(int(reg_2_val, 16), int(reg_3_val, 16), int(reg_4_val, 16))def reg_02_returnPressed(self):self.reg_03.setFocus()def reg_03_returnPressed(self):self.reg_04.setFocus()def retranslateUi(self, Form):_translate = QtCore.QCoreApplication.translateForm.setWindowTitle(_translate("Form", "计算器1"))self.btn_cal_h_l_s.setText(_translate("Form", "calculate"))self.label.setText(_translate("Form", "x=0x"))self.label_2.setText(_translate("Form", "y=0x"))self.label_3.setText(_translate("Form", "z=0x"))import sys
class MainWindow(QMainWindow):def __init__(self):super(MainWindow,self).__init__()self.ui=Ui_Form()self.ui.setupUi(self)
app=QApplication(sys.argv)
window=MainWindow()
window.show()
sys.exit(app.exec())