输出:
输出是一个带有按钮的计算器,当你点击其中任意一个按钮时,它的值就会显示在显示屏上 。
现在我们的计算器只剩下两个按钮就能完整 , 一个是重置按钮用于清除屏幕,另一个是等号(=)按钮 , 用于计算表达式并将结果显示在屏幕上 。
六、为计算器添加重置和等号按钮【五分钟内完成个性化Python GUI计算器搭建】from tkinter import Tk, Entry, Button, StringVarclass Calculator:def __init__(self, master):master.title('Simple Calculator')master.geometry('360x260+0+0')master.config(bg='#438')master.resizable(False, False)self.equation = StringVar()self.entry_value = https://www.isolves.com/it/cxkf/yy/Python/2024-01-05/''Entry(width = 28,bg='lightblue', font = ('Times', 16), textvariable = self.equation).place(x=0,y=0)Button(width=8, text = '(', relief ='flat', command=lambda:self.show('(')).place(x=0,y=50)Button(width=8, text = ')', relief ='flat', command=lambda:self.show(')')).place(x=90, y=50)Button(width=8, text = '%', relief ='flat', command=lambda:self.show('%')).place(x=180, y=50)Button(width=8, text = '1', relief ='flat', command=lambda:self.show(1)).place(x=0,y=90)Button(width=8, text = '2', relief ='flat', command=lambda:self.show(2)).place(x=90,y=90)Button(width=8, text = '3', relief ='flat', command=lambda:self.show(3)).place(x=180,y=90)Button(width=8, text = '4', relief ='flat', command=lambda:self.show(4)).place(x=0,y=130)Button(width=8, text = '5', relief ='flat', command=lambda:self.show(5)).place(x=90,y=130)Button(width=8, text = '6', relief ='flat', command=lambda:self.show(6)).place(x=180,y=130)Button(width=8, text = '7', relief ='flat', command=lambda:self.show(7)).place(x=0,y=170)Button(width=8, text = '8', relief ='flat', command=lambda:self.show(8)).place(x=180,y=170)Button(width=8, text = '9', relief ='flat', command=lambda:self.show(9)).place(x=90,y=170)Button(width=8, text = '0', relief ='flat', command=lambda:self.show(0)).place(x=0,y=210)Button(width=8, text = '.', relief ='flat', command=lambda:self.show('.')).place(x=90,y=210)Button(width=8, text = '+', relief ='flat', command=lambda:self.show('+')).place(x=270,y=90)Button(width=8, text = '-', relief ='flat', command=lambda:self.show('-')).place(x=270,y=130)Button(width=8, text = '/', relief ='flat', command=lambda:self.show('/')).place(x=270,y=170)Button(width=8, text = 'x', relief ='flat', command=lambda:self.show('*')).place(x=270,y=210)Button(width=8, text = '=', bg='green', relief ='flat', command=self.solve).place(x=180, y=210)Button(width=8, text = 'AC', relief ='flat', command=self.clear).place(x=270,y=50)def show(self, value):self.entry_value +=str(value)self.equation.set(self.entry_value)def clear(self):self.entry_value = ''self.equation.set(self.entry_value)def solve(self):result = eval(self.entry_value)self.equation.set(result)root = Tk()calculator = Calculator(root)root.mainloop()输出:
七、结语在短短的五分钟内 , 我们成功地使用Tkinter库搭建了一个Python GUI计算器 。这个计算器可以进行基本的数学运算,并为用户提供了友好的交互体验 。
搭建一个GUI计算器不仅仅是一个有趣的项目 , 它还展示了Python的强大和灵活性 。希望对你有所帮助 , 并激励你进一步探索和开发更多有趣的GUI应用程序!
推荐阅读
- 狼吞虎咽、毫无形象,《繁花》唐嫣吃年糕,给内娱女星上了生动一
- 范志毅自曝出演繁花内幕:拒绝王家卫好几次,老婆拍板让他出山
- 轮毂内侧的贴片什么用,锐志原装轮毂内的小贴片是什麽
- 35岁内地模特自曝左手截肢,公开术后照片,面带笑容称已接受现状
- 假唱事件再次发酵,圈内一半人中招...
- 该咋滴才可以看电脑内存,电脑主机开机时检测不到内存怎么办
- 宋喆近照曝光!现身县城发廊,暴瘦憔悴,曝复出无望圈内无人敢用
- 揭秘娱乐圈7位内地过气艺人现状,看看谁最让人同情?
- 曝周杰伦出轨风波升级,更多内幕被扒,徐若瑄也被牵连!
- 内娱开年第一个翻车,没想到是他
