脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python使用tkinter实现简单计算器

python使用tkinter实现简单计算器

2021-01-10 00:16wiz_333 Python

这篇文章主要为大家详细介绍了python使用tkinter实现简单计算器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

?
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
136
137
138
139
140
class Counter:
 #引入tkinter
 import tkinter as tk
 #引入消息弹窗模块
 import tkinter.messagebox as mbox
 
 #初始化Counter
 def __init__(self):
 #生成一个窗口对象
 self.window = self.tk.Tk()
 #命名窗口对象的显示title
 self.window.title('计算器')
 #设置窗口的大小
 self.window.minsize(240, 325)
 self.window.maxsize(240, 325)
 #是否清空显示框判定参数
 self.is_init_lable = False
 #设置菜单
 self.set_menu()
 #设置显示框
 self.lable_show = self.tk.Label(text='', anchor='se', font=('黑体', 30), fg='black')
 self.lable_show.place(x=0, y=0, width=240, height=80)
 #设置按钮组件
 self.set_buttons()
 #将窗口放入主消息队列
 self.window.mainloop()
 
 #设置菜单
 def set_menu(self):
 #创建总菜单
 menubar = self.tk.Menu(self.window)
 #创建一个下拉菜单,并且加入文件菜单
 filemenu = self.tk.Menu(menubar, tearoff=0)
 #创建下来菜单的选项
 filemenu.add_command(label="退出计算器", command=self.window.quit)
 #print author的函数
 def show_author():
  self.mbox.showinfo(message='Wiz333@XDL 2017')
 filemenu.add_command(label="作者", command=show_author)
 #将文件菜单作为下拉菜单添加到总菜单中,并且将命名为操作
 menubar.add_cascade(label="操作", menu=filemenu)
 #显示总菜单
 self.window.config(menu=menubar)
 
 #设置按钮组件
 def set_buttons(self):
 #7
 btn7 = self.tk.Button(text='7', bd=2, font='黑体')
 btn7.place(x=0, y=90, width=60, height=40)
 #8
 btn8 = self.tk.Button(text='8', bd=2, font='黑体')
 btn8.place(x=60, y=90, width=60, height=40)
 #9
 btn9 = self.tk.Button(text='9', bd=2, font='黑体')
 btn9.place(x=120, y=90, width=60, height=40)
 #+
 btn_jia = self.tk.Button(text='+', bd=2, font='黑体')
 btn_jia.place(x=180, y=90, width=60, height=40)
 #4
 btn4 = self.tk.Button(text='4', bd=2, font='黑体')
 btn4.place(x=0, y=130, width=60, height=40)
 #5
 btn5 = self.tk.Button(text='5', bd=2, font='黑体')
 btn5.place(x=60, y=130, width=60, height=40)
 #6
 btn6 = self.tk.Button(text='6', bd=2, font='黑体')
 btn6.place(x=120, y=130, width=60, height=40)
 #-
 btn_jian = self.tk.Button(text='-', bd=2, font='黑体')
 btn_jian.place(x=180, y=130, width=60, height=40)
 #1
 btn1 = self.tk.Button(text='1', bd=2, font='黑体')
 btn1.place(x=0, y=170, width=60, height=40)
 #2
 btn2 = self.tk.Button(text='2', bd=2, font='黑体')
 btn2.place(x=60, y=170, width=60, height=40)
 #3
 btn3 = self.tk.Button(text='3', bd=2, font='黑体')
 btn3.place(x=120, y=170, width=60, height=40)
 #*
 btn_cheng = self.tk.Button(text='*', bd=2, font='黑体')
 btn_cheng.place(x=180, y=170, width=60, height=40)
 #0
 btn0 = self.tk.Button(text='0', bd=2, font='黑体')
 btn0.place(x=0, y=210, width=120, height=40)
 #.
 btn_point = self.tk.Button(text='.', bd=2, font='黑体')
 btn_point.place(x=120, y=210, width=60, height=40)
 #/
 btn_chu = self.tk.Button(text='/', bd=2, font='黑体')
 btn_chu.place(x=180, y=210, width=60, height=40)
 #取消
 btn_cancel = self.tk.Button(text='C', bd=2, font='黑体')
 btn_cancel.place(x=0, y=250, width=60, height=40)
 #确定
 btn_ok = self.tk.Button(text='=', bd=2, font='黑体')
 btn_ok.place(x=60, y=250, width=180, height=40)
 #绑定Button的点击事件
 btn7.bind_class('Button', '<Button-1>', self.click_button)
 
 #绑定Button的点击事件
 def click_button(self,e):
 #判断是否是新的运算,如果是则清空显示框
 if self.is_init_lable:
  self.lable_show['text'] = ''
  self.is_init_lable = False
 #label_show显示的累加
 font = e.widget['text']
 self.lable_show['text'] += font
 #异常捕获
 try:
  #判定运算符号重复的时候,使用最后输入的符号
  if self.lable_show['text'][-1] in ['+','-','*','/'] and self.lable_show['text'][-2] in ['+','-','*','/']:
  header = self.lable_show['text'][:-2]
  footer = self.lable_show['text'][-1]
  self.lable_show['text'] = header+footer
 except:
  pass
 
 #普通计算
 if e.widget['text'] == '=':
  try:
  res = eval(self.lable_show['text'][:-1])
  #print(res)
  #小数点取到9位
  self.lable_show['text'] = str(round(float(res), 5))
  self.isinit = True
  except ZeroDivisionError:
  #除法时,除数不能为0
  self.mbox.showerror(message='除法计算时!除数不能为0!')
  except:
  self.mbox.showerror(message='算式有误')
 #取消当前输入的字符
 if e.widget['text'] == 'C':
  cancel_res = self.lable_show['text'][:-2]
  self.lable_show['text'] = cancel_res
 
 
#实例化计算器对象
wiz = Counter()

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

原文链接:http://blog.csdn.net/wizblack/article/details/78932258

延伸 · 阅读

精彩推荐