我是靠谱客的博主 传统期待,最近开发中收集的这篇文章主要介绍pyqt5计算器,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

pyqt5计算器

大家好,我是Te,今天我们用pyqt5做一个简易的计算器


首先导入库

import sys
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

创建一个类,名为Calculator,继承QWidget

class Calculator(QWidget):
def __init__(self,parent=None):
super().__init__(parent)
#后面需要用到的变量
self.text = ''
self.ifo = ''
self.flag = False
#调用初始化函数和主函数
self.init()
self.algorithm()

初始化部分


def init(self):
QToolTip.setFont(QFont('SansSerif', 10))
self.resize(1000,625)
self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint)
self.setFixedSize(self.width(), self.height())
self.setWindowTitle('计算器')
self.win_offQL = QLabel(self)
self.win_offQL.setGeometry(QtCore.QRect(970, 10, 21, 21))
self.win_offQL.setStyleSheet("border-radius:10px;n""background-color: rgb(170, 0, 0);")
self.win_offQL.setText("")
self.win_off = QPushButton(self)
self.win_off.setGeometry(QtCore.QRect(970, 10, 21, 21))
self.win_off.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
self.win_off.setStyleSheet("QPushButton{background:#ff5500;border-radius:10px;}QPushButton:hover{background:red;}")
self.win_off.setText("")
self.win_off.clicked.connect(sys.exit)

计算器主函数部分,定义多个组件并创建跳转函数


def algorithm(self):
def TZ(text):
if text != "\0":
if text != "=":
self.text += text
self.l1.setText(self.text)
else:
self.l1.clear()
try:
self.l1.setText(str(eval(self.text)))
except SyntaxError:
self.l1.setText(self.ifo)
self.flag = True
if not self.flag:
self.ifo = self.text
else:
self.flag = False
self.text = ''
else:
self.text = ''
self.l1.clear()
self.l1 = QLabel(self)
self.l1.setFrameShape(QFrame.Box)
self.l1.setLineWidth(3)
self.l1.setMidLineWidth(1)
self.l1.setGeometry(420, 100, 200, 50)
self.l1.show()
self.L_1 = QPushButton(self)
self.L_2 = QPushButton(self)
self.L_3 = QPushButton(self)
self.L_4 = QPushButton(self)
self.L_5 = QPushButton(self)
self.L_6 = QPushButton(self)
self.L_7 = QPushButton(self)
self.L_8 = QPushButton(self)
self.L_9 = QPushButton(self)
self.L_0 = QPushButton(self)
self.L_X_ = QPushButton(self)
self.L_chu_ = QPushButton(self)
self.L_jia_ = QPushButton(self)
self.L_jian_ = QPushButton(self)
self.L_kuo1_ = QPushButton(self)
self.L_kuo2_ = QPushButton(self)
self.Yes_No = QPushButton(self)
self.gui_zero = QPushButton(self)
self._dian_ = QPushButton(self)
lh = 80
wh = 30
x = 250
self.L_1.setGeometry(x, 200, lh, wh)
self.L_1.setText('1')
self.L_1.clicked.connect(lambda: TZ('1'))
self.L_2.setGeometry(x + 100, 200, lh, wh)
self.L_2.setText('2')
self.L_2.clicked.connect(lambda: TZ('2'))
self.L_3.setGeometry(x + 100 + 100, 200, lh, wh)
self.L_3.setText('3')
self.L_3.clicked.connect(lambda: TZ('3'))
self.L_4.setGeometry(x + 100 + 100 + 100, 200, lh, wh)
self.L_4.setText('4')
self.L_4.clicked.connect(lambda: TZ('4'))
self.L_5.setGeometry(x + 100 + 100 + 100 + 100, 200, lh, wh)
self.L_5.setText('5')
self.L_5.clicked.connect(lambda: TZ('5'))
self.L_6.setGeometry(x, 300, lh, wh)
self.L_6.setText('6')
self.L_6.clicked.connect(lambda: TZ('6'))
self.L_7.setGeometry(x + 100, 300, lh, wh)
self.L_7.setText('7')
self.L_7.clicked.connect(lambda: TZ('7'))
self.L_8.setGeometry(x + 100 + 100, 300, lh, wh)
self.L_8.setText('8')
self.L_8.clicked.connect(lambda: TZ('8'))
self.L_9.setGeometry(x + 100 + 100 + 100, 300, lh, wh)
self.L_9.setText('9')
self.L_9.clicked.connect(lambda: TZ('9'))
self.L_0.setGeometry(x + 100 + 100 + 100 + 100, 300, lh, wh)
self.L_0.setText('0')
self.L_0.clicked.connect(lambda: TZ('0'))
self.L_X_.setGeometry(x, 400, lh, wh)
self.L_X_.setText('*')
self.L_X_.clicked.connect(lambda: TZ('*'))
self.L_chu_.setGeometry(x + 100, 400, lh, wh)
self.L_chu_.setText('/')
self.L_chu_.clicked.connect(lambda: TZ('/'))
self.L_jia_.setGeometry(x + 100 + 100, 400, lh, wh)
self.L_jia_.setText('+')
self.L_jia_.clicked.connect(lambda: TZ('+'))
self.L_jian_.setGeometry(x + 100 + 100 + 100, 400, lh, wh)
self.L_jian_.setText('-')
self.L_jian_.clicked.connect(lambda: TZ('-'))
self.L_kuo1_.setGeometry(x + 100 + 100 + 100 + 100, 400, lh, wh)
self.L_kuo1_.setText('(')
self.L_kuo1_.clicked.connect(lambda: TZ('('))
self.L_kuo2_.setGeometry(x, 500, lh, wh)
self.L_kuo2_.setText(')')
self.L_kuo2_.clicked.connect(lambda: TZ(')'))
self.Yes_No.setGeometry(x + 100, 500, lh, wh)
self.Yes_No.setText('=')
self.Yes_No.clicked.connect(lambda: TZ('='))
self.gui_zero.setGeometry(x + 100 + 100, 500, lh, wh)
self.gui_zero.setText("\0")
self.gui_zero.clicked.connect(lambda: TZ("\0"))
self._dian_.setGeometry(x + 100 + 100 + 100, 500, lh, wh)
self._dian_.setText('.')
self._dian_.clicked.connect(lambda: TZ('.'))

这里遇到个困难,“TZ"函数需要传入一个参数Text,可是”…".clicked.connect()函数里调用的函数不能带括号以及里面的参数,所以采用了lambda表达式,即可解决此问题,lambda表达式学习入口:
https://blog.csdn.net/imzoer/article/details/8667176

最后主函数入口

if __name__ == '__main__':
def run():
app = QApplication(sys.argv)
UI = Calculator()
UI.show()
sys.exit(app.exec_())
run()

ok,又结束了,还是那句话,喜欢我的文章请麻烦点一个爱心收藏加关注,爱你么么哒,我们下次再见!

最后

以上就是传统期待为你收集整理的pyqt5计算器的全部内容,希望文章能够帮你解决pyqt5计算器所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部