PyQt5实现简易计算器

本文实例为大家分享了PyQt5实现简易计算器的具体代码,供大家参考,具体内容如下

效果图:

界面代码

calc_interface.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'calc_interface.ui'
#
# Created by: PyQt5 UI code generator 5.8.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
 def setupUi(self, MainWindow):
  MainWindow.setObjectName("MainWindow")
  MainWindow.resize(647, 675)
  self.centralwidget = QtWidgets.QWidget(MainWindow)
  self.centralwidget.setObjectName("centralwidget")
  self.b_1 = QtWidgets.QPushButton(self.centralwidget)
  self.b_1.setGeometry(QtCore.QRect(10, 330, 71, 71))
  self.b_1.setObjectName("b_1")
  self.b_2 = QtWidgets.QPushButton(self.centralwidget)
  self.b_2.setGeometry(QtCore.QRect(80, 330, 71, 71))
  self.b_2.setObjectName("b_2")
  self.b_3 = QtWidgets.QPushButton(self.centralwidget)
  self.b_3.setGeometry(QtCore.QRect(150, 330, 71, 71))
  self.b_3.setObjectName("b_3")
  self.b_6 = QtWidgets.QPushButton(self.centralwidget)
  self.b_6.setGeometry(QtCore.QRect(150, 400, 71, 71))
  self.b_6.setObjectName("b_6")
  self.b_4 = QtWidgets.QPushButton(self.centralwidget)
  self.b_4.setGeometry(QtCore.QRect(10, 400, 71, 71))
  self.b_4.setObjectName("b_4")
  self.b_5 = QtWidgets.QPushButton(self.centralwidget)
  self.b_5.setGeometry(QtCore.QRect(80, 400, 71, 71))
  self.b_5.setObjectName("b_5")
  self.b_8 = QtWidgets.QPushButton(self.centralwidget)
  self.b_8.setGeometry(QtCore.QRect(80, 470, 71, 71))
  self.b_8.setObjectName("b_8")
  self.b_9 = QtWidgets.QPushButton(self.centralwidget)
  self.b_9.setGeometry(QtCore.QRect(150, 470, 71, 71))
  self.b_9.setObjectName("b_9")
  self.b_7 = QtWidgets.QPushButton(self.centralwidget)
  self.b_7.setGeometry(QtCore.QRect(10, 470, 71, 71))
  self.b_7.setObjectName("b_7")
  self.b_eq = QtWidgets.QPushButton(self.centralwidget)
  self.b_eq.setGeometry(QtCore.QRect(150, 540, 211, 71))
  self.b_eq.setObjectName("b_eq")
  self.b_mod = QtWidgets.QPushButton(self.centralwidget)
  self.b_mod.setGeometry(QtCore.QRect(220, 400, 71, 71))
  self.b_mod.setObjectName("b_mod")
  self.b_bra_l = QtWidgets.QPushButton(self.centralwidget)
  self.b_bra_l.setGeometry(QtCore.QRect(220, 330, 71, 71))
  self.b_bra_l.setObjectName("b_bra_l")
  self.b_div = QtWidgets.QPushButton(self.centralwidget)
  self.b_div.setGeometry(QtCore.QRect(220, 260, 71, 71))
  self.b_div.setObjectName("b_div")
  self.b_add = QtWidgets.QPushButton(self.centralwidget)
  self.b_add.setGeometry(QtCore.QRect(10, 260, 71, 71))
  self.b_add.setObjectName("b_add")
  self.b_sub = QtWidgets.QPushButton(self.centralwidget)
  self.b_sub.setGeometry(QtCore.QRect(80, 260, 71, 71))
  self.b_sub.setObjectName("b_sub")
  self.b_mul = QtWidgets.QPushButton(self.centralwidget)
  self.b_mul.setGeometry(QtCore.QRect(150, 260, 71, 71))
  self.b_mul.setObjectName("b_mul")
  self.b_pow = QtWidgets.QPushButton(self.centralwidget)
  self.b_pow.setGeometry(QtCore.QRect(290, 260, 71, 71))
  self.b_pow.setObjectName("b_pow")
  self.b_pai = QtWidgets.QPushButton(self.centralwidget)
  self.b_pai.setGeometry(QtCore.QRect(290, 400, 71, 71))
  self.b_pai.setObjectName("b_pai")
  self.b_bra_r = QtWidgets.QPushButton(self.centralwidget)
  self.b_bra_r.setGeometry(QtCore.QRect(290, 330, 71, 71))
  self.b_bra_r.setObjectName("b_bra_r")
  self.l_hist = QtWidgets.QListWidget(self.centralwidget)
  self.l_hist.setGeometry(QtCore.QRect(380, 10, 256, 601))
  self.l_hist.setObjectName("l_hist")
  self.e_view = QtWidgets.QTextEdit(self.centralwidget)
  self.e_view.setGeometry(QtCore.QRect(10, 10, 351, 231))
  font = QtGui.QFont()
  font.setFamily("Adobe Caslon Pro Bold")
  font.setPointSize(22)
  font.setBold(True)
  font.setWeight(75)
  self.e_view.setFont(font)
  self.e_view.setObjectName("e_view")
  self.b_0 = QtWidgets.QPushButton(self.centralwidget)
  self.b_0.setGeometry(QtCore.QRect(80, 540, 71, 71))
  self.b_0.setObjectName("b_0")
  self.b_pt = QtWidgets.QPushButton(self.centralwidget)
  self.b_pt.setGeometry(QtCore.QRect(10, 540, 71, 71))
  self.b_pt.setObjectName("b_pt")
  self.b_del = QtWidgets.QPushButton(self.centralwidget)
  self.b_del.setGeometry(QtCore.QRect(220, 470, 71, 71))
  self.b_del.setObjectName("b_del")
  self.b_clc = QtWidgets.QPushButton(self.centralwidget)
  self.b_clc.setGeometry(QtCore.QRect(290, 470, 71, 71))
  self.b_clc.setObjectName("b_clc")
  MainWindow.setCentralWidget(self.centralwidget)
  self.menubar = QtWidgets.QMenuBar(MainWindow)
  self.menubar.setGeometry(QtCore.QRect(0, 0, 647, 30))
  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.b_1.setText(_translate("MainWindow", "1"))
  self.b_2.setText(_translate("MainWindow", "2"))
  self.b_3.setText(_translate("MainWindow", "3"))
  self.b_6.setText(_translate("MainWindow", "6"))
  self.b_4.setText(_translate("MainWindow", "4"))
  self.b_5.setText(_translate("MainWindow", "5"))
  self.b_8.setText(_translate("MainWindow", "8"))
  self.b_9.setText(_translate("MainWindow", "9"))
  self.b_7.setText(_translate("MainWindow", "7"))
  self.b_eq.setText(_translate("MainWindow", "="))
  self.b_mod.setText(_translate("MainWindow", "%"))
  self.b_bra_l.setText(_translate("MainWindow", "("))
  self.b_div.setText(_translate("MainWindow", "/"))
  self.b_add.setText(_translate("MainWindow", "+"))
  self.b_sub.setText(_translate("MainWindow", "-"))
  self.b_mul.setText(_translate("MainWindow", "*"))
  self.b_pow.setText(_translate("MainWindow", "^"))
  self.b_pai.setText(_translate("MainWindow", "π"))
  self.b_bra_r.setText(_translate("MainWindow", ")"))
  self.b_0.setText(_translate("MainWindow", "0"))
  self.b_pt.setText(_translate("MainWindow", "."))
  self.b_del.setText(_translate("MainWindow", "删除"))
  self.b_clc.setText(_translate("MainWindow", "清空"))

主逻辑代码

calc_main.py

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from calc_interface import Ui_MainWindow
import os,sys

global e_view

pluginsPath='PyQt5/Qt/plugins'
if os.path.exists(pluginsPath):#指定插件路径。源码运行时不会生效,打包后运行检测到路径,加载插件
 QApplication.addLibraryPath(pluginsPath)

class MyMainWindow(QMainWindow, Ui_MainWindow):

 def forge_link(self):
  self.b_0.clicked.connect(self.button_event(0))
  self.b_1.clicked.connect(self.button_event(1))
  self.b_2.clicked.connect(self.button_event(2))
  self.b_3.clicked.connect(self.button_event(3))
  self.b_4.clicked.connect(self.button_event(4))
  self.b_5.clicked.connect(self.button_event(5))
  self.b_6.clicked.connect(self.button_event(6))
  self.b_7.clicked.connect(self.button_event(7))
  self.b_8.clicked.connect(self.button_event(8))
  self.b_9.clicked.connect(self.button_event(9))
  self.b_add.clicked.connect(self.button_event('+'))
  self.b_sub.clicked.connect(self.button_event('-'))
  self.b_mul.clicked.connect(self.button_event('*'))
  self.b_div.clicked.connect(self.button_event('/'))
  self.b_pow.clicked.connect(self.button_event('**'))
  self.b_bra_l.clicked.connect(self.button_event('('))
  self.b_bra_r.clicked.connect(self.button_event(')'))
  self.b_mod.clicked.connect(self.button_event('%'))
  self.b_pai.clicked.connect(self.button_event('3.1415926'))
  self.b_pt.clicked.connect(self.button_event('.'))
  self.b_del.clicked.connect(self.delete_event)
  self.b_clc.clicked.connect(self.clear_event)
  self.b_eq.clicked.connect(self.calc_complish)

 def __init__(self, parent=None):
  super(MyMainWindow, self).__init__(parent)
  self.setupUi(self)
  self.forge_link() #连接槽函数

 def button_event(self,arg):
  # print(dir(self.e_view))
  global e_view
  e_view=self.e_view
  def fun():  #返回一个自定义的槽函数
   global e_view
   txt = e_view.toPlainText()
   e_view.setText(txt + str(arg))
  return fun

 def calc_complish(self):
  txt=self.e_view.toPlainText()
  ans=''
  try:
   ans=str(eval(txt))
  except BaseException:
   ans='MathError'
  # print(ans)
  self.clear_event()
  self.e_view.setText(ans)
  self.l_hist.addItem(txt+'='+ans)

 def clear_event(self):
  self.e_view.setText('')

 def delete_event(self):
  txt = self.e_view.toPlainText()
  txt=txt[:len(txt)-1]
  self.e_view.setText(txt)

if __name__ == '__main__':
 app=QApplication(sys.argv)
 myWin=MyMainWindow()
 myWin.show()
 sys.exit(app.exec())

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Python只用40行代码编写的计算器实例

    本文实例讲述了Python只用40行代码编写的计算器.分享给大家供大家参考,具体如下: 效果图: 代码: from tkinter import * reset=True def buttonCallBack(event): global label global reset num=event.widget['text'] if num=='C': label['text']="0" return if num in "=": label['text']=str(

  • 仅用50行代码实现一个Python编写的计算器的教程

     简介 在这篇文章中,我将向大家演示怎样向一个通用计算器一样解析并计算一个四则运算表达式.当我们结束的时候,我们将得到一个可以处理诸如 1+2*-(-3+2)/5.6+3样式的表达式的计算器了.当然,你也可以将它拓展的更为强大. 我本意是想提供一个简单有趣的课程来讲解 语法分析 和 正规语法(编译原理内容).同时,介绍一下PlyPlus,这是一个我断断续续改进了好几年的语法解析 接口.作为这个课程的附加产物,我们最后会得到完全可替代eval()的一个安全的四则运算器. 如果你想在自家的电脑上试试

  • Python PyQt5实现的简易计算器功能示例

    本文实例讲述了Python PyQt5实现的简易计算器功能.分享给大家供大家参考,具体如下: 这里剩下计算函数(self.calculator)未实现,有兴趣的朋友可以实现它 [知识点] 1.利用循环添加按钮部件,及给每个按钮设置信号/槽 2.给按钮设置固定大小:button.setFixedSize(QtCore.QSize(60,30)) 3.取事件的的发送者(此例为各个按钮)的文本: self.sender().text() [效果图] [源代码] import sys from PyQt

  • python正则表达式之作业计算器

    作业:计算器开发 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等类似公式后,必须自己解析里面的(),+,-,*,/符号和公式,运算后得出结果,结果必须与真实的计算器所得出的结果一致 一.说明: 有一点bug就是不能计算幂次方,如:'6**6'会报错 该计算器思路: 1.没用使用递归,先找出并计算所有括号里的公式,再计算乘除

  • 利用Tkinter(python3.6)实现一个简单计算器

    前言 上机实践课程开始了,嗯,老师来了之后念了下PPT,然后说:开始做吧......... 然后就开始了Python的GUI之路,以前没接触过PYthon的可视化界面(虽然这样很不明智) 但是现在做起来感觉写小工具还挺方便的,当时搜到的第一个库便是Tkinter就直接开始写了 后来发现QT很不错的样子,下个实验就用QT吧.然后关于Tkinter(python3.6) 计算器源码 ennn.....有的命名不规范....... 首先对于python中栈的实现是通过list的方式模拟 pop()出栈

  • 基于wxpython开发的简单gui计算器实例

    本文实例讲述了基于wxpython开发的简单gui计算器.分享给大家供大家参考.具体如下: # wxCalc1 a simple GUI calculator using wxPython # created with the Boa Constructor which generates all the GUI components # all I had to do is add some code for each button click event # Boa free from: h

  • Python开发的实用计算器完整实例

    本文实例讲述了Python开发的实用计算器.分享给大家供大家参考,具体如下: 实现功能:图形界面PyQt,输入框,+,-,*,/ :乘方 ,开方 ,取余,清零. 1. Python代码: #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Author : Mr.LiuYC Created on 2014-09-30 E-Mail : liuyanchen0725@gmail.com Introduction: 简易计算器 实现图形界面PyQt,输

  • python 简易计算器程序,代码就几行

    代码: 复制代码 代码如下: import os while True: dynamic = input('输入计算表达式:') if dynamic != 'cls': try: result = eval(dynamic) print('计算结果:'+str(result)) except: print('计算表达式输入有误!') else: command = 'cls' os.system(command)

  • Python实现简单的四则运算计算器

    一.算法 1.算法的主要思想就是将一个中缀表达式(Infix expression)转换成便于处理的后缀表达式(Postfix expression),然后借助于栈这个简单的数据结构,计算出表达式的结果. 2.关于如何讲普通的表达式转换成后缀表达式,以及如何处理后缀表达式并计算出结果的具体算法描述不在此叙述了,书上有详细的说明. 二.简易计算器 使用说明 使用该计算器类的简单示例如下: # usage c = Calculator() print('result: {:f}'.formart(c

  • 基于python的Tkinter实现一个简易计算器

    本文实例介绍了基于python的Tkinter实现简易计算器的详细代码,分享给大家供大家参考,具体内容如下 第一种:使用python 的 Tkinter实现一个简易计算器 #coding:utf-8 from Tkinter import * import time root = Tk() def cacl(input_str): if "x" in input_str: ret = input_str.split("x") return int(ret[0]) *

随机推荐