鱼C论坛

 找回密码
 立即注册
查看: 1845|回复: 8

[作品展示] 简易计算器,tkinter练手未优化

[复制链接]
发表于 2020-9-7 23:56:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 gdmao002 于 2020-11-25 09:20 编辑


                               
登录/注册后可看大图

以下是练手的简单计算器,未经过优化。写的不好海涵

  1. import tkinter
  2. import re


  3. def del_zero(*args):
  4.     global num
  5.     c()
  6.     r = re.match(r"(\d+\.?\d*)([/*+-]{0,2})(\d*?\.?\d*)(=?)$", num)
  7.     if r:
  8.         if r.group(1) != '':  # 第1个单元有值2个字符00-09判断
  9.             if len(num) == 2:
  10.                 if len(r.group(1)) == 2:
  11.                     if r.group(1)[0] == '0' and r.group(1)[1].isdigit():
  12.                         num = r.group(1)[1]
  13.         if r.group(2) != '':  # 第2个单元满足不为空的时候
  14.             if '.' == r.group(1)[-1]:  # 判断第1个单元如果有1'.'在后的处理
  15.                 num = num[:-1]
  16.             elif len(r.group(1)) >= 3:  # 判断第1个单元如果有1'.'在中间 对于多'0'处理
  17.                 if r.group(1)[0] == '0' and r.group(1)[1] == '.':
  18.                     # 处理.后一堆0 # 剩余的0个数
  19.                     for i in range(2, len(r.group(1))):
  20.                         if r.group(1)[i] != '0':
  21.                             break
  22.                     else:
  23.                         num = '0' + r.group(2)
  24.             if len(r.group(2)) == 2:  # 判断运算符号个数
  25.                 num = r.group(1) + r.group(2)[-1]
  26.         if r.group(3) != '':  # 判断第3个单元不为空的输入
  27.             if len(r.group(3)) == 1:  # 第3个单元有值2个字符00-09判断
  28.                 if not r.group(3)[0].isdigit():
  29.                     num = num[:-1]
  30.             elif len(r.group(3)) == 2:
  31.                 if r.group(3)[0] == '0' and r.group(3)[1].isdigit():
  32.                     num = r.group(1) + r.group(2) + r.group(3)[1]
  33.             elif len(r.group(3)) >= 3:
  34.                 if r.group(3)[-1] == '.' and r.group(3)[-2] == '.':  # 处理第三单元.的个数
  35.                     num = num[:-1]
  36.         if r.group(4) == '=':  # 第4个单元为'='时
  37.             if r.group(3) != '':
  38.                 if r.group(3)[-1] == '.':  # 判断第3个单元.在最后的处理
  39.                     num = num[:-1]
  40.                 elif len(r.group(3)) >= 3:  # 判断第3个单元如果出现0.00000这类的处理
  41.                     if r.group(3)[0] == '0' and r.group(3)[1] == '.':
  42.                         for i in range(2, len(r.group(3))):  # 处理.后一堆0 # 剩余的0个数
  43.                             if r.group(3)[i] != '0':
  44.                                 break
  45.                         else:
  46.                             num = r.group(1) + r.group(2) + '0'
  47.                 if r.group(2) != '' and r.group(3)[-1].isdigit():  # 如果=号左边为数字 开始结算
  48.                     cal_1()
  49.             else:
  50.                 num = num[:-1]  # 第3单元空 第2单元空
  51.         v.set(num)
  52.     else:
  53.         num = num[:-1]
  54.         v.set(num)


  55. def fun0():
  56.     global num
  57.     c()
  58.     num += '0'
  59.     del_zero()


  60. def fun1():
  61.     global num
  62.     c()
  63.     num += '1'
  64.     del_zero()


  65. def fun2():
  66.     global num
  67.     c()
  68.     num += '2'
  69.     del_zero()


  70. def fun3():
  71.     global num
  72.     c()
  73.     num += '3'
  74.     del_zero()


  75. def fun4():
  76.     global num
  77.     c()
  78.     num += '4'
  79.     del_zero()


  80. def fun5():
  81.     global num
  82.     c()
  83.     num += '5'
  84.     del_zero()


  85. def fun6():
  86.     global num
  87.     c()
  88.     num += '6'
  89.     del_zero()


  90. def fun7():
  91.     global num
  92.     c()
  93.     num += '7'
  94.     del_zero()


  95. def fun8():
  96.     global num
  97.     c()
  98.     num += '8'
  99.     del_zero()


  100. def fun9():
  101.     global num
  102.     c()
  103.     num += '9'
  104.     del_zero()


  105. def fun():
  106.     global num
  107.     c()
  108.     num += '.'
  109.     del_zero()


  110. def fun_add():
  111.     global num
  112.     c()
  113.     num += '+'
  114.     del_zero()


  115. def fun_sub():
  116.     global num
  117.     c()
  118.     num += '-'
  119.     del_zero()


  120. def fun_chen():
  121.     global num
  122.     c()
  123.     num += '*'
  124.     del_zero()


  125. def fun_chu():
  126.     global num
  127.     c()
  128.     num += r'/'
  129.     del_zero()


  130. def fun_del():
  131.     global num
  132.     c()
  133.     if num != '':
  134.         num = num[:-1]
  135.         v.set(num)


  136. def fun_c():
  137.     global num
  138.     c()
  139.     if num != '':
  140.         num = ''
  141.     v.set(num)


  142. def fun_ce():
  143.     global num
  144.     c()
  145.     if num == '':
  146.         v.set(num)
  147.     else:
  148.         r = re.match(r"(\d+\.?\d*)([/*+-]{0,2})(\d*?\.?\d*)(=?)$", num)
  149.         if r:
  150.             if r.group(3) != '':
  151.                 num = r.group(1) + r.group(2)
  152.                 v.set(num)


  153. # 状态函数 负责清空结算
  154. def c():
  155.     global num, count
  156.     if count == 1:
  157.         num = ''
  158.         count = 0


  159. def cal_1():
  160.     global num, count
  161.     r = re.match(r"(\d+\.?\d*)([/*+-]{0,2})(\d*?\.?\d*)(=?)$", num)
  162.     sum1 = 0
  163.     if '.' in r.group(1):
  164.         x = float(r.group(1))
  165.     else:
  166.         x = int(r.group(1))
  167.     if '.' in r.group(3):
  168.         y = float(r.group(3))
  169.     else:
  170.         y = int(r.group(3))
  171.     if '+' == r.group(2):
  172.         sum1 = x + y
  173.     elif '-' == r.group(2):
  174.         sum1 = x - y
  175.     elif '*' == r.group(2):
  176.         sum1 = x * y
  177.     elif '/' == r.group(2):
  178.         if r.group(3) == '0':
  179.             num = r.group(1) + r.group(2)
  180.             return -1
  181.         else:
  182.             sum1 = x / y
  183.     num += str(sum1)

  184.     count = 1


  185. def cal():
  186.     global num
  187.     num += '='
  188.     del_zero()


  189. def callback(event):
  190.     if event.char == '0':
  191.         fun0()
  192.     elif event.char == '1':
  193.         fun1()
  194.     elif event.char == '2':
  195.         fun2()
  196.     elif event.char == '3':
  197.         fun3()
  198.     elif event.char == '4':
  199.         fun4()
  200.     elif event.char == '5':
  201.         fun5()
  202.     elif event.char == '6':
  203.         fun6()
  204.     elif event.char == '7':
  205.         fun7()
  206.     elif event.char == '8':
  207.         fun8()
  208.     elif event.char == '9':
  209.         fun9()
  210.     elif event.char == '.':
  211.         fun()
  212.     elif event.char == '+':
  213.         fun_add()
  214.     elif event.char == '-':
  215.         fun_sub()
  216.     elif event.char == '*':
  217.         fun_chen()
  218.     elif event.char == r'/':
  219.         fun_chu()
  220.     elif event.char == '\r':
  221.         cal()
  222.     else:
  223.         pass


  224. root = tkinter.Tk()
  225. root.title('简易计算器')
  226. root.resizable(0, 0)

  227. frame2 = tkinter.Frame(root)
  228. frame2.grid(row=0, column=1, padx=5, pady=5)

  229. num = ''
  230. count = 0
  231. v = tkinter.StringVar()
  232. label2 = tkinter.Label(frame2, textvariable=v).grid(row=0, column=1, padx=5, pady=5)

  233. frame1 = tkinter.Frame(root)
  234. frame1.grid(row=1, column=1, padx=5, pady=5)

  235. root.bind('<Key>', callback)
  236. root.focus_set()

  237. tkinter.Button(frame1, text='←', width=4, command=fun_del).grid(row=2, column=0, padx=2, pady=5)
  238. tkinter.Button(frame1, text='C', width=4, command=fun_c).grid(row=2, column=1, padx=2, pady=5)
  239. tkinter.Button(frame1, text='CE', width=4, command=fun_ce).grid(row=2, column=2, padx=2, pady=5)
  240. tkinter.Button(frame1, text='/', width=4, command=fun_chu).grid(row=2, column=3, padx=5, pady=5)
  241. tkinter.Button(frame1, text='7', width=4, command=fun7).grid(row=3, column=0, padx=5, pady=2)
  242. tkinter.Button(frame1, text='8', width=4, command=fun8).grid(row=3, column=1, padx=2, pady=2)
  243. tkinter.Button(frame1, text='9', width=4, command=fun9).grid(row=3, column=2, padx=2, pady=2)
  244. tkinter.Button(frame1, text='*', width=4, command=fun_chen).grid(row=3, column=3, padx=5, pady=2)
  245. tkinter.Button(frame1, text='4', width=4, command=fun4).grid(row=4, column=0, padx=5, pady=2)
  246. tkinter.Button(frame1, text='5', width=4, command=fun5).grid(row=4, column=1, padx=2, pady=2)
  247. tkinter.Button(frame1, text='6', width=4, command=fun6).grid(row=4, column=2, padx=2, pady=2)
  248. tkinter.Button(frame1, text='-', width=4, command=fun_sub).grid(row=4, column=3, padx=5, pady=2)
  249. tkinter.Button(frame1, text='1', width=4, command=fun1).grid(row=5, column=0, padx=5, pady=2)
  250. tkinter.Button(frame1, text='2', width=4, command=fun2).grid(row=5, column=1, padx=2, pady=2)
  251. tkinter.Button(frame1, text='3', width=4, command=fun3).grid(row=5, column=2, padx=2, pady=2)
  252. tkinter.Button(frame1, text='+', width=4, command=fun_add).grid(row=5, column=3, padx=5, pady=2)
  253. tkinter.Button(frame1, text='0', width=4, command=fun0).grid(row=6, column=0, padx=5, pady=5)
  254. tkinter.Button(frame1, text='.', width=4, command=fun).grid(row=6, column=1, padx=2, pady=5)
  255. tkinter.Button(frame1, text='=', width=10, command=cal).grid(row=6, column=2, padx=5, pady=5, columnspan=2)
  256. root.mainloop()
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-9-8 00:05:45 | 显示全部楼层
如有什么疏漏欢迎指正
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-8 10:28:35 | 显示全部楼层
不错,比我强,写了这么长
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-8 11:06:00 | 显示全部楼层
厉害厉害,能写这么长
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-22 15:21:07 | 显示全部楼层
所有的r = re.match(r'(\d+\.?\d*)([/*+-]{0,2})(\d*?\.?\d*)(=?), num)好像都漏了一个单引号
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-25 09:07:14 | 显示全部楼层
叼辣条闯世界 发表于 2020-11-22 15:21
所有的r = re.match(r'(\d+\.?\d*)([/*+-]{0,2})(\d*?\.?\d*)(=?), num)好像都漏了一个单引号

奇怪我直接用我的py文件复制过来的,怎么漏了呢。我刚打开了py文件我这里都是好的,那我重新在黏贴一遍吧,感谢你的提醒!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-25 09:16:49 | 显示全部楼层
gdmao002 发表于 2020-11-25 09:07
奇怪我直接用我的py文件复制过来的,怎么漏了呢。我刚打开了py文件我这里都是好的,那我重新在黏贴一遍吧 ...

奇怪我复制了一遍后 再次发现还是少了单引号!惊了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-25 09:22:13 | 显示全部楼层
叼辣条闯世界 发表于 2020-11-22 15:21
所有的r = re.match(r'(\d+\.?\d*)([/*+-]{0,2})(\d*?\.?\d*)(=?), num)好像都漏了一个单引号

我把单引号换双引号就可以复制进来了。现在就可以了,感谢你的提醒!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-31 21:46:48 | 显示全部楼层
厉害厉害,能写这么长
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 15:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表