关键词搜索

源码搜索 ×
×

Python实用工具,tkinter模块,Python实现简易版计算器

发布2021-08-06浏览528次

详情内容

前言:

Python制作一个简单的计算器呗。
让我们愉快地开始吧~

image.png

开发工具

Python版本:3.6.4
相关模块:
python自带的tkinter和math模块即可。
环境搭建

安装Python并添加到环境变量即可。

原理简介

先利用tkinter搭建基本的计算器界面,这里就不详细说明了,请自行参考相关文档,如:

https://docs.python.org/zh-cn/3/library/tk.html

按键功能介绍如下:
0-9:数字键
.:小数点
+:加法
-:减法
*:乘法
/:除法
=:计算结果
1/x:取倒数
%:取余
del:退格键
CE:清空当前
C:清空所有
+/-:取反
sqrt:开根号
MC:将目前记忆的数字归零
MR:将当前计算出来的数字呈现出来
MS:无视目前记忆多少数字,直接以当前数字取代记忆中的数字
M+:记忆当前数字
M-:记忆当前数字(负数形式)
因为直接调用了math库和python的eval函数,所以功能实现起来很简单,这里主要讲几个实现过程中需要注意的细节。

(1)退格操作

显示框中只有一个数字的时候,使用退格键后应当使显示框显示数字0,而不是空。

(2)非法操作

把将0作为分母或对负数开根号等操作归入非法操作,避免程序抛出异常。

(3)设置当前显示字符最大长度

因为显示框长度有限,所以能显示的字符数量也有限,因此需要设置当前显示字符最大长度。比如用户不可以无限制地连续输入一连串数字,最终结果只能保留前多少位等等。

部分代码

  1. '''Demo'''
  2. def Demo():
  3. root.minsize(320, 420)
  4. root.title('Calculator')
  5. # 布局
  6. # --文本框
  7. label = tkinter.Label(root, textvariable=CurrentShow, bg='black', anchor='e', bd=5, fg='white', font=('楷体', 20))
  8. label.place(x=20, y=50, width=280, height=50)
  9. # --第一行
  10. # ----Memory clear
  11. button1_1 = tkinter.Button(text='MC', bg='#666', bd=2, command=lambda:pressOperator('MC'))
  12. button1_1.place(x=20, y=110, width=50, height=35)
  13. # ----Memory read
  14. button1_2 = tkinter.Button(text='MR', bg='#666', bd=2, command=lambda:pressOperator('MR'))
  15. button1_2.place(x=77.5, y=110, width=50, height=35)
  16. # ----Memory save
  17. button1_3 = tkinter.Button(text='MS', bg='#666', bd=2, command=lambda:pressOperator('MS'))
  18. button1_3.place(x=135, y=110, width=50, height=35)
  19. # ----Memory +
  20. button1_4 = tkinter.Button(text='M+', bg='#666', bd=2, command=lambda:pressOperator('M+'))
  21. button1_4.place(x=192.5, y=110, width=50, height=35)
  22. # ----Memory -
  23. button1_5 = tkinter.Button(text='M-', bg='#666', bd=2, command=lambda:pressOperator('M-'))
  24. button1_5.place(x=250, y=110, width=50, height=35)
  25. # --第二行
  26. # ----删除单个数字
  27. button2_1 = tkinter.Button(text='del', bg='#666', bd=2, command=lambda:delOne())
  28. button2_1.place(x=20, y=155, width=50, height=35)
  29. # ----清除当前显示框内所有数字
  30. button2_2 = tkinter.Button(text='CE', bg='#666', bd=2, command=lambda:clearCurrent())
  31. button2_2.place(x=77.5, y=155, width=50, height=35)
  32. # ----清零(相当于重启)
  33. button2_3 = tkinter.Button(text='C', bg='#666', bd=2, command=lambda:clearAll())
  34. button2_3.place(x=135, y=155, width=50, height=35)
  35. # ----取反
  36. button2_4 = tkinter.Button(text='+/-', bg='#666', bd=2, command=lambda:pressOperator('+/-'))
  37. button2_4.place(x=192.5, y=155, width=50, height=35)
  38. # ----开根号
  39. button2_5 = tkinter.Button(text='sqrt', bg='#666', bd=2, command=lambda:pressOperator('sqrt'))
  40. button2_5.place(x=250, y=155, width=50, height=35)
  41. # --第三行
  42. # ----7
  43. button3_1 = tkinter.Button(text='7', bg='#bbbbbb', bd=2, command=lambda:pressNumber('7'))
  44. button3_1.place(x=20, y=200, width=50, height=35)
  45. # ----8
  46. button3_2 = tkinter.Button(text='8', bg='#bbbbbb', bd=2, command=lambda:pressNumber('8'))
  47. button3_2.place(x=77.5, y=200, width=50, height=35)
  48. # ----9
  49. button3_3 = tkinter.Button(text='9', bg='#bbbbbb', bd=2, command=lambda:pressNumber('9'))
  50. button3_3.place(x=135, y=200, width=50, height=35)
  51. # ----除
  52. button3_4 = tkinter.Button(text='/', bg='#708069', bd=2, command=lambda:pressOperator('/'))
  53. button3_4.place(x=192.5, y=200, width=50, height=35)
  54. # ----取余
  55. button3_5 = tkinter.Button(text='%', bg='#708069', bd=2, command=lambda:pressOperator('%'))
  56. button3_5.place(x=250, y=200, width=50, height=35)
  57. # --第四行
  58. # ----4
  59. button4_1 = tkinter.Button(text='4', bg='#bbbbbb', bd=2, command=lambda:pressNumber('4'))
  60. button4_1.place(x=20, y=245, width=50, height=35)
  61. # ----5
  62. button4_2 = tkinter.Button(text='5', bg='#bbbbbb', bd=2, command=lambda:pressNumber('5'))
  63. button4_2.place(x=77.5, y=245, width=50, height=35)
  64. # ----6
  65. button4_3 = tkinter.Button(text='6', bg='#bbbbbb', bd=2, command=lambda:pressNumber('6'))
  66. button4_3.place(x=135, y=245, width=50, height=35)
  67. # ----乘
  68. button4_4 = tkinter.Button(text='*', bg='#708069', bd=2, command=lambda:pressOperator('*'))
  69. button4_4.place(x=192.5, y=245, width=50, height=35)
  70. # ----取导数
  71. button4_5 = tkinter.Button(text='1/x', bg='#708069', bd=2, command=lambda:pressOperator('1/x'))
  72. button4_5.place(x=250, y=245, width=50, height=35)
  73. # --第五行
  74. # ----3
  75. button5_1 = tkinter.Button(text='3', bg='#bbbbbb', bd=2, command=lambda:pressNumber('3'))
  76. button5_1.place(x=20, y=290, width=50, height=35)
  77. # ----2
  78. button5_2 = tkinter.Button(text='2', bg='#bbbbbb', bd=2, command=lambda:pressNumber('2'))
  79. button5_2.place(x=77.5, y=290, width=50, height=35)
  80. # ----1
  81. button5_3 = tkinter.Button(text='1', bg='#bbbbbb', bd=2, command=lambda:pressNumber('1'))
  82. button5_3.place(x=135, y=290, width=50, height=35)
  83. # ----减
  84. button5_4 = tkinter.Button(text='-', bg='#708069', bd=2, command=lambda:pressOperator('-'))
  85. button5_4.place(x=192.5, y=290, width=50, height=35)
  86. # ----等于
  87. button5_5 = tkinter.Button(text='=', bg='#708069', bd=2, command=lambda:pressOperator('='))
  88. button5_5.place(x=250, y=290, width=50, height=80)
  89. # --第六行
  90. # ----0
  91. button6_1 = tkinter.Button(text='0', bg='#bbbbbb', bd=2, command=lambda:pressNumber('0'))
  92. button6_1.place(x=20, y=335, width=107.5, height=35)
  93. # ----小数点
  94. button6_2 = tkinter.Button(text='.', bg='#bbbbbb', bd=2, command=lambda:pressDP())
  95. button6_2.place(x=135, y=335, width=50, height=35)
  96. # ----加
  97. button6_3 = tkinter.Button(text='+', bg='#708069', bd=2, command=lambda:pressOperator('+'))
  98. button6_3.place(x=192.5, y=335, width=50, height=35)
  99. root.mainloop()
  100. if __name__ == '__main__':
  101. Demo()

文章到这vb.net教程里就结束了,感谢c#教程你的观看,下篇python教程文章分享Turtle库制作简易时钟

为了感谢读者们,我想把我最近收藏的一些编程干货分享给大家,回馈每一个读者,希望能帮到你们。

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载