使用PySide6这个强大的Python库来创建一个简单的计算器小程序。PySide6是Qt应用程序框架的Python绑定,非常适合开发跨平台的GUI应用程序。下面我们就开始吧!
环境准备
Python——PyQt5在PyCharm的配置与应用(保姆级教程)_pycharm pyqt5-CSDN博客教找在什么地方配置插件,Pycharm配置Pyside6-Designer,UIC,rcc工具_pyside6-uic-CSDN博客
QtDesigner是创建新的ui项目。
QtDesignerEdit是修改已存在的ui项目
设计UI界面
使用Qt Designer设计一个简单的计算器界面,包含以下元素:
- 一个用于显示输入内容的文本框(lineEdit)
- 一些按钮,分别代表数字0-9、加、减、乘、除、小数点、退格、清空和等于
设计完成后,保存为compute.ui
文件。接下来,我们需要将UI文件转换为Python代码。
这样,我们就得到了一个ui
目录下的compute.py
文件,其中包含了UI界面的Python代码。
编写计算器逻辑
下面是我们的计算器小程序的主体代码:
from PySide6.QtWidgets import QWidget, QApplication
from ui.compute import Ui_Form
class MyWidget(QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.bind()
self.result = ''
def bind(self):
self.pushButton_0.clicked.connect(lambda: self.addNumber('0'))
self.pushButton_1.clicked.connect(lambda: self.addNumber('1'))
self.pushButton_2.clicked.connect(lambda: self.addNumber('2'))
self.pushButton_3.clicked.connect(lambda: self.addNumber('3'))
self.pushButton_4.clicked.connect(lambda: self.addNumber('4'))
self.pushButton_5.clicked.connect(lambda: self.addNumber('5'))
self.pushButton_6.clicked.connect(lambda: self.addNumber('6'))
self.pushButton_7.clicked.connect(lambda: self.addNumber('7'))
self.pushButton_8.clicked.connect(lambda: self.addNumber('8'))
self.pushButton_9.clicked.connect(lambda: self.addNumber('9'))
self.pushButton_add.clicked.connect(lambda: self.addNumber('+'))
self.pushButton_minus.clicked.connect(lambda: self.addNumber('-'))
self.pushButton_plus.clicked.connect(lambda: self.addNumber('*'))
self.pushButton_divide.clicked.connect(lambda: self.addNumber('/'))
self.pushButton_dot.clicked.connect(lambda: self.addNumber('.'))
self.pushButton_back.clicked.connect(lambda: self.back())
self.pushButton_clear.clicked.connect(lambda: self.cleared())
self.pushButton_compute.clicked.connect(lambda: self.compute())
def addNumber(self, num):
# self.lineEdit.clear()
self.result += num
self.setLineEditText()
def compute(self):
numResult = eval(self.result)
self.lineEdit_2.setText(str(numResult))
def cleared(self):
self.result = ''
self.setLineEditText()
def back(self):
if self.result:
self.result = self.result[:-1]
self.setLineEditText()
def setLineEditText(self):
self.lineEdit.setText(self.result)
self.lineEdit_2.clear()
if __name__ == '__main__':
app = QApplication([])
window = MyWidget()
window.show()
app.exec()
在这段代码中,我们定义了一个MyWidget
类,它继承自QWidget
和Ui_Form
。Ui_Form
是从UI文件转换而来的,包含了界面元素的定义。
初始化与UI设置
在MyWidget
类的构造函数__init__
中,我们首先调用super().__init__()
来初始化基类QWidget
。然后,我们调用setupUi(self)
来设置UI界面,这是由Qt Designer生成的代码提供的。接着,我们调用bind()
方法来绑定按钮的点击事件,并初始化一个空字符串self.result
来存储用户输入的表达式。
绑定按钮点击事件
在bind
方法中,我们为每个按钮绑定了点击事件,这样当用户点击按钮时,会触发相应的函数。
添加数字或运算符
addNumber
方法用于将用户点击的数字或运算符添加到结果字符串中,并更新显示。
计算结果
compute
方法使用Python内置的eval
函数来计算结果字符串的值,并将结果显示在界面上。
清空输入和退格
cleared
和back
方法分别用于清空输入和删除最后一个字符。
设置lineEdit文本
setLineEditText
方法用于更新lineEdit的文本内容。
ui生成的代码:
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'compute.ui'
##
## Created by: Qt User Interface Compiler version 6.7.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QHBoxLayout, QLineEdit, QPushButton,
QSizePolicy, QVBoxLayout, QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(609, 428)
self.lineEdit = QLineEdit(Form)
self.lineEdit.setObjectName(u"lineEdit")
self.lineEdit.setGeometry(QRect(20, 20, 561, 81))
self.layoutWidget = QWidget(Form)
self.layoutWidget.setObjectName(u"layoutWidget")
self.layoutWidget.setGeometry(QRect(20, 180, 567, 184))
self.verticalLayout = QVBoxLayout(self.layoutWidget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_4 = QHBoxLayout()
self.horizontalLayout_4.setObjectName(u"horizontalLayout_4")
self.pushButton_clear = QPushButton(self.layoutWidget)
self.pushButton_clear.setObjectName(u"pushButton_clear")
self.horizontalLayout_4.addWidget(self.pushButton_clear)
self.pushButton_back = QPushButton(self.layoutWidget)
self.pushButton_back.setObjectName(u"pushButton_back")
self.horizontalLayout_4.addWidget(self.pushButton_back)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.horizontalLayout = QHBoxLayout()
self.horizontalLayout.setObjectName(u"horizontalLayout")
self.pushButton_7 = QPushButton(self.layoutWidget)
self.pushButton_7.setObjectName(u"pushButton_7")
self.horizontalLayout.addWidget(self.pushButton_7)
self.pushButton_8 = QPushButton(self.layoutWidget)
self.pushButton_8.setObjectName(u"pushButton_8")
self.horizontalLayout.addWidget(self.pushButton_8)
self.pushButton_9 = QPushButton(self.layoutWidget)
self.pushButton_9.setObjectName(u"pushButton_9")
self.horizontalLayout.addWidget(self.pushButton_9)
self.pushButton_minus = QPushButton(self.layoutWidget)
self.pushButton_minus.setObjectName(u"pushButton_minus")
self.horizontalLayout.addWidget(self.pushButton_minus)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QHBoxLayout()
self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
self.pushButton_4 = QPushButton(self.layoutWidget)
self.pushButton_4.setObjectName(u"pushButton_4")
self.horizontalLayout_2.addWidget(self.pushButton_4)
self.pushButton_5 = QPushButton(self.layoutWidget)
self.pushButton_5.setObjectName(u"pushButton_5")
self.horizontalLayout_2.addWidget(self.pushButton_5)
self.pushButton_6 = QPushButton(self.layoutWidget)
self.pushButton_6.setObjectName(u"pushButton_6")
self.horizontalLayout_2.addWidget(self.pushButton_6)
self.pushButton_plus = QPushButton(self.layoutWidget)
self.pushButton_plus.setObjectName(u"pushButton_plus")
self.horizontalLayout_2.addWidget(self.pushButton_plus)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QHBoxLayout()
self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
self.pushButton_1 = QPushButton(self.layoutWidget)
self.pushButton_1.setObjectName(u"pushButton_1")
self.horizontalLayout_3.addWidget(self.pushButton_1)
self.pushButton_2 = QPushButton(self.layoutWidget)
self.pushButton_2.setObjectName(u"pushButton_2")
self.horizontalLayout_3.addWidget(self.pushButton_2)
self.pushButton_3 = QPushButton(self.layoutWidget)
self.pushButton_3.setObjectName(u"pushButton_3")
self.horizontalLayout_3.addWidget(self.pushButton_3)
self.pushButton_divide = QPushButton(self.layoutWidget)
self.pushButton_divide.setObjectName(u"pushButton_divide")
self.horizontalLayout_3.addWidget(self.pushButton_divide)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_5 = QHBoxLayout()
self.horizontalLayout_5.setObjectName(u"horizontalLayout_5")
self.pushButton_0 = QPushButton(self.layoutWidget)
self.pushButton_0.setObjectName(u"pushButton_0")
self.horizontalLayout_5.addWidget(self.pushButton_0)
self.pushButton_dot = QPushButton(self.layoutWidget)
self.pushButton_dot.setObjectName(u"pushButton_dot")
self.horizontalLayout_5.addWidget(self.pushButton_dot)
self.pushButton_compute = QPushButton(self.layoutWidget)
self.pushButton_compute.setObjectName(u"pushButton_compute")
self.horizontalLayout_5.addWidget(self.pushButton_compute)
self.pushButton_add = QPushButton(self.layoutWidget)
self.pushButton_add.setObjectName(u"pushButton_add")
self.horizontalLayout_5.addWidget(self.pushButton_add)
self.verticalLayout.addLayout(self.horizontalLayout_5)
self.lineEdit_2 = QLineEdit(Form)
self.lineEdit_2.setObjectName(u"lineEdit_2")
self.lineEdit_2.setGeometry(QRect(20, 120, 561, 41))
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"\u8ba1\u7b97", None))
self.pushButton_clear.setText(QCoreApplication.translate("Form", u"\u6e05\u7a7a", None))
self.pushButton_back.setText(QCoreApplication.translate("Form", u"\u56de\u9000", None))
self.pushButton_7.setText(QCoreApplication.translate("Form", u"7", None))
self.pushButton_8.setText(QCoreApplication.translate("Form", u"8", None))
self.pushButton_9.setText(QCoreApplication.translate("Form", u"9", None))
self.pushButton_minus.setText(QCoreApplication.translate("Form", u"-", None))
self.pushButton_4.setText(QCoreApplication.translate("Form", u"4", None))
self.pushButton_5.setText(QCoreApplication.translate("Form", u"5", None))
self.pushButton_6.setText(QCoreApplication.translate("Form", u"6", None))
self.pushButton_plus.setText(QCoreApplication.translate("Form", u"*", None))
self.pushButton_1.setText(QCoreApplication.translate("Form", u"1", None))
self.pushButton_2.setText(QCoreApplication.translate("Form", u"2", None))
self.pushButton_3.setText(QCoreApplication.translate("Form", u"3", None))
self.pushButton_divide.setText(QCoreApplication.translate("Form", u"/", None))
self.pushButton_0.setText(QCoreApplication.translate("Form", u"0", None))
self.pushButton_dot.setText(QCoreApplication.translate("Form", u".", None))
self.pushButton_compute.setText(QCoreApplication.translate("Form", u"\u8ba1\u7b97", None))
self.pushButton_add.setText(QCoreApplication.translate("Form", u"+", None))
# retranslateUi
如果你的代码没有问题,你应该能看到一个简单的计算器界面。现在,你可以尝试使用这个计算器进行简单的数学运算了!
支持更复杂的运算
如果你的计算器需要支持更复杂的运算,比如三角函数或对数,你可以添加更多的按钮和对应的处理逻辑。
改进UI
你可以使用Qt Designer来进一步改进UI,比如添加字体、颜色、图标等,使计算器看起来更美观。
结语
通过这个简单的计算器项目,我们学习了如何使用PySide6创建一个GUI应用程序。虽然这个计算器相对简单,但它展示了GUI编程的基本概念,包括事件处理、UI布局和状态管理。
在未来的学习中,你可以探索更多PySide6的功能,比如信号和槽的高级用法、自定义窗口小部件、以及多线程等,来创建更复杂和功能丰富的应用程序。
希望这个教程能够帮助你迈出使用PySide6进行GUI编程的第一步。如果你有任何问题或想法,欢迎在评论区留言交流!