PyQt5实现简易计算器

时间:2022-12-11 09:22:42

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

效果图:

PyQt5实现简易计算器

界面代码

calc_interface.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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())

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

原文链接:https://blog.csdn.net/TQCAI666/article/details/80532034