当前位置:   article > 正文

用python中GUI制作简易计算器

用python中GUI制作简易计算器

话不多说,直接上代码:

  1. import tkinter as tk
  2. win3 = tk.Tk()
  3. win3.title('简易计算器')
  4. win3.geometry('400x300')
  5. result_var = tk.StringVar()
  6. def digit_click(num):
  7. resul_show.insert('end',num)
  8. def digit(x):
  9. resul_show.insert('end',x)
  10. def re():
  11. opt_str = resul_show.get()
  12. y = eval(opt_str)
  13. resul_show.insert('end','='+ str(y))
  14. def cls():
  15. resul_show.delete(0,100)
  16. resul_show = tk.Entry(win3,font='Arial 22',width=15,textvariable=result_var)
  17. btn9 = tk.Button(win3, text='9', width=6, height=2,command=lambda :digit_click('9'))
  18. btn8 = tk.Button(win3, text='8', width=6, height=2,command=lambda :digit_click('8'))
  19. btn7 = tk.Button(win3, text='7', width=6, height=2,command=lambda :digit_click('7'))
  20. btn6 = tk.Button(win3, text='6', width=6, height=2,command=lambda :digit_click('6'))
  21. btn5 = tk.Button(win3, text='5', width=6, height=2,command=lambda :digit_click('5'))
  22. btn4 = tk.Button(win3, text='4', width=6, height=2,command=lambda :digit_click('4'))
  23. btn3 = tk.Button(win3, text='3', width=6, height=2,command=lambda :digit_click('3'))
  24. btn2 = tk.Button(win3, text='2', width=6, height=2,command=lambda :digit_click('2'))
  25. btn1 = tk.Button(win3, text='1', width=6, height=2,command=lambda :digit_click('1'))
  26. btn0 = tk.Button(win3, text='0', width=6, height=2,command=lambda :digit_click('0'))
  27. btn_point = tk.Button(win3, text='.', width=6, height=2, command=lambda :digit('.'))
  28. btn_add = tk.Button(win3, text='+', width=6, height=2, command=lambda :digit('+'))
  29. btn_equal = tk.Button(win3, text='=',width=6, height=2,command=re)
  30. btn_sub = tk.Button(win3, text='-',width=6, height=2, command=lambda :digit('-'))
  31. btn_cls = tk.Button(win3, text='Cls',width=6, height=5,command=lambda :cls())
  32. resul_show.grid(row=0,column=0,columnspan=4)
  33. btn9.grid(row=1,column=0)
  34. btn8.grid(row=1, column=1)
  35. btn7.grid(row=1, column=2)
  36. btn6.grid(row=2, column=0)
  37. btn5.grid(row=2, column=1)
  38. btn4.grid(row=2, column=2)
  39. btn3.grid(row=3, column=0)
  40. btn2.grid(row=3, column=1)
  41. btn1.grid(row=3, column=2)
  42. btn0.grid(row=4, column=0)
  43. btn_cls.grid(row=1,rowspan=2,column=3)
  44. btn_sub.grid(row=3,column=3)
  45. btn_point.grid(row=4, column=1)
  46. btn_add.grid(row=4, column=2)
  47. btn_equal.grid(row=4, column=3)
  48. win3.mainloop()

 运行效果图如下:

 如有不对之处,欢迎各位的指正,

有更好的方法,欢迎评论区交流。

 如有不懂之处在评论区写出,感谢观看!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/275631
推荐阅读
相关标签
  

闽ICP备14008679号