基于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
|
import re import tkinter import tkinter.messagebox root = tkinter.Tk() # 设置窗口大小和位置 root.geometry( '300x270+400+100' ) # 不允许改变窗口大小 root.resizable( False , False ) # 设置窗口标题 root.title( '简易计算机' .center( 25 )) # 设置用来显示信息的文本框,并设置为只读 contentVar = tkinter.StringVar(root, '0' ) contentEntry = tkinter.Entry(root, textvariable = contentVar) contentEntry[ 'state' ] = 'readonly' contentEntry.place(x = 10 , y = 10 , width = 280 , height = 20 ) # 按钮通用代码 def buttonClick(btn): content = contentVar.get() # 如果已有内容是以小数点开头的,前面加0 if content.startswith( '.' ): content = '0' + content # 根据不同按钮作出相应的处理 if btn in '0123456789' : if content = = '0' : content = '' content + = btn elif btn = = '.' : lastPart = re.split(r '\+|-|\*|/' , content)[ - 1 ] if '.' in lastPart: tkinter.messagebox.showerror( '错误' , '小数点太多了' ) return else : content + = btn elif btn = = 'C' : content = '0' elif btn = = '=' : try : # 对输入的表达式求值 content = str ( eval (content)) except : tkinter.messagebox.showerror( '错误' , '表达式错误' ) return elif btn in operators or btn in operators1: if content.endswith(operators) or content.endswith(operators1): tkinter.messagebox.showerror( '错误' , '不允许存在连续运算符' ) return content + = btn elif btn = = 'Sqrt' : n = content.split( '.' ) if all ( map ( lambda x: x.isdigit(), n)): content = eval (content) * * 0.5 else : tkinter.messagebox.showerror( '错误' , '表达式错误' ) return contentVar. set (content) # 放置Clear按钮和“=”按钮 btnClear = tkinter.Button(root, text = 'C' , command = lambda : buttonClick( 'C' )) btnClear.place(x = 20 , y = 40 , width = 50 , height = 20 ) btnCompute = tkinter.Button(root, text = '=' , command = lambda : buttonClick( '=' )) btnCompute.place(x = 230 , y = 230 , width = 50 , height = 20 ) # 放置10个数字、小数点和计算平方根的按钮 digits = list ( '7894561230.' ) + [ 'Sqrt' ] index = 0 for row in range ( 4 ): for col in range ( 3 ): d = digits[index] index + = 1 btnDigit = tkinter.Button(root, text = d, command = lambda x = d: buttonClick(x)) btnDigit.place(x = 20 + col * 70 , y = 80 + row * 50 , width = 50 , height = 20 ) # 放置运算符按钮 index = 0 operators1 = ( '/' , '**' , '//' ) for index, operator1 in enumerate (operators1): btnOperator = tkinter.Button(root, text = operator1, command = lambda x = operator1: buttonClick(x)) btnOperator.place(x = 230 , y = 80 + index * 50 , width = 50 , height = 20 ) operators = ( '+' , '-' , '*' ) for index, operator in enumerate (operators): if operator = = '+' or operator = = '-' or operator = = '*' : btnOperator = tkinter.Button(root, text = operator, command = lambda x = operator: buttonClick(x)) btnOperator.place(x = 90 + index * 70 , y = 40 , width = 50 , height = 20 ) root.mainloop() |
代码运行结果(简易计算机):
重点
1、选择需要使用的图形界面(窗口,文本框,按钮);
2、图形界面参数的设置(位置、大小、初始化参数等);
3、重复运算符或小数点报错(当然你也可以编写程序直接不报错但不写入重复运算符或小数点);
4、运算结果的实现,使用这个简易计算器,实际上其实就是先输入一串字符串表达式,然后使用eval() 函数用来执行该字符串表达式,并返回表达式的值,该值就是我们要计算的结果。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_45440931/article/details/121503881