温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在python中使用tkinter实现一个计算器

发布时间:2021-03-29 17:01:22 来源:亿速云 阅读:135 作者:Leah 栏目:开发技术

怎么在python中使用tkinter实现一个计算器?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

具体内容如下

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()

看完上述内容,你们掌握怎么在python中使用tkinter实现一个计算器的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI