鱼C论坛

 找回密码
 立即注册
查看: 5995|回复: 33

[技术交流] tkinter——一个加减乘除的计算器

[复制链接]
发表于 2019-6-29 08:55:57 | 显示全部楼层 |阅读模式

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

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

x
游客,如果您要查看本帖隐藏内容请回复

刚学习tkinter ,写了一个加减乘除的计算器。还不够完美,需要加油鸭!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-6-29 09:01:39 | 显示全部楼层
不能连续除,我去
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-8 12:51:40 | 显示全部楼层
看看哟,感谢分享
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-24 11:28:06 | 显示全部楼层
6666
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-30 12:40:19 | 显示全部楼层
有漏洞,看看我的
  1. # -*- coding:utf-8 -*-
  2. """
  3. 此代码使用UTF-8编码
  4. 在Windows7上测试通过
  5. 作者ZiHeng
  6. QQ:3318448676
  7. 微信:b3318448676
  8. """

  9. from tkinter import *
  10. from tkinter import messagebox
  11. from tkinter import ttk
  12. import re
  13. import pyperclip

  14. JieGuo = ''
  15. root = Tk()
  16. root.title('计算器')  # 设置窗口名字
  17. root.iconbitmap(r'calc.ico')  # 设置窗口图标


  18. # 开始定义输入函数

  19. def input1(*args): inputbox.insert(INSERT, '1')


  20. def input2(*args): inputbox.insert(INSERT, '2')


  21. def input3(*args): inputbox.insert(INSERT, '3')


  22. def input4(*args): inputbox.insert(INSERT, '4')


  23. def input5(*args): inputbox.insert(INSERT, '5')


  24. def input6(*args): inputbox.insert(INSERT, '6')


  25. def input7(*args): inputbox.insert(INSERT, '7')


  26. def input8(*args): inputbox.insert(INSERT, '8')


  27. def input9(*args): inputbox.insert(INSERT, '9')


  28. def input0(*args): inputbox.insert(INSERT, '0')


  29. def inputplus(*args): inputbox.insert(INSERT, '+')


  30. def inputminus(*args): inputbox.insert(INSERT, '-')


  31. def inputtimes(*args): inputbox.insert(INSERT, '*')


  32. def inputdiv(*args): inputbox.insert(INSERT, '/')


  33. def inputleft(*args): inputbox.insert(INSERT, '(')


  34. def inputright(*args): inputbox.insert(INSERT, ')')


  35. def inputdian(*args): inputbox.insert(INSERT, '.')


  36. def inputc(*args): inputbox.delete(1.0, 'end')


  37. def inputdel(*args):
  38.     beifen = inputbox.get(1.0, 'end')
  39.     beifen = beifen[0:-2]
  40.     inputbox.delete(1.0, 'end')
  41.     inputbox.insert(END, beifen)
  42.     del beifen


  43. def inputok(*args):
  44.     number = inputbox.get('1.0', 'end')
  45.     inputbox.delete('1.0', 'end')
  46.     qukuohao(number)
  47.     inputbox.insert(END, JieGuo)


  48. # 输入函数定义结束

  49. # 定义菜单函数

  50. def copy(*args):
  51.     pyperclip.copy(inputbox.get(1.0, 'end'))
  52.     messagebox.showinfo('提示', '复制成功!')


  53. def pasta(*args):
  54.     messagebox.showinfo('提示', '粘贴成功!')
  55.     inputbox.insert(END, pyperclip.paste())


  56. def settings():
  57.     settingsbox = Toplevel(root)
  58.     settingsbox.title('设置')
  59.     settingsbox.resizable(width=False, height=False)

  60.     group0 = LabelFrame(settingsbox, text='帮助', padx=5, pady=5)
  61.     group0.pack(pady=10, padx=10, side=LEFT)
  62.     label0 = Label(group0, text='这是一个功能待完善的计算器\n它还有许多功能没有添加'
  63.                                 '\n也有许多Bug没有修复\n欢迎反馈漏洞(联系方式见关于)', jus=LEFT)
  64.     label0.pack()

  65.     def helpbtbox():
  66.         helpForBt = Toplevel(settingsbox)
  67.         helpForBt.title('快捷键操作帮助')
  68.         # 帮助文字
  69.         ttk.Label(helpForBt, text='<----0---->,输入0   | ', jus=LEFT).grid(row=0, column=0)
  70.         ttk.Label(helpForBt, text='<----1---->,输入1   | ', jus=LEFT).grid(row=1, column=0)
  71.         ttk.Label(helpForBt, text='<----2---->,输入2   | ', jus=LEFT).grid(row=2, column=0)
  72.         ttk.Label(helpForBt, text='<----3---->,输入3   | ', jus=LEFT).grid(row=3, column=0)
  73.         ttk.Label(helpForBt, text='<----4---->,输入4   | ', jus=LEFT).grid(row=4, column=0)
  74.         ttk.Label(helpForBt, text='<----5---->,输入5   | ', jus=LEFT).grid(row=5, column=0)
  75.         ttk.Label(helpForBt, text='<----6---->,输入6   | ', jus=LEFT).grid(row=6, column=0)
  76.         ttk.Label(helpForBt, text='<----7---->,输入7   | ', jus=LEFT).grid(row=7, column=0)
  77.         ttk.Label(helpForBt, text='<----8---->,输入8', jus=LEFT).grid(row=0, column=1)
  78.         ttk.Label(helpForBt, text='<----9---->,输入9', jus=LEFT).grid(row=1, column=1)
  79.         ttk.Label(helpForBt, text='<-退格键->,退格', jus=LEFT).grid(row=2, column=1)
  80.         ttk.Label(helpForBt, text='<-Delete->,清空 ', jus=LEFT).grid(row=3, column=1)
  81.         ttk.Label(helpForBt, text='<Ctrl + C>,复制', jus=LEFT).grid(row=4, column=1)
  82.         ttk.Label(helpForBt, text='<Ctrl + V>,粘贴', jus=LEFT).grid(row=5, column=1)
  83.         ttk.Label(helpForBt, text='<--回车-->,运算', jus=LEFT).grid(row=6, column=1)
  84.         ttk.Label(helpForBt, text='<Alt + F4>,退出', jus=LEFT).grid(row=7, column=1)
  85.         # 帮助文字结束
  86.         # 禁止调整窗口大小
  87.         helpForBt.resizable(width=False, height=False)
  88.         # 完成

  89.     ttk.Button(group0, text='快捷键操作帮助', command=helpbtbox).pack()

  90.     group1 = LabelFrame(settingsbox, text='捐赠和关于', padx=5, pady=5)
  91.     group1.pack(pady=10, padx=10)
  92.     label1 = Label(group1, text='支付宝:Chinese.he.amber@gmail.com\n'
  93.                                 '微信:b3318448676\nQQ:3318448676\n作者:ZiHeng', jus=LEFT)
  94.     label1.grid(row=0, column=0, sticky='W')

  95.     def about():
  96.         aboutbox = Toplevel(settingsbox)
  97.         aboutbox.title('关于')
  98.         aboutphoto = PhotoImage(file='calc.gif')
  99.         aboutbox.resizable(width=False, height=False)

  100.         l1 = ttk.Label(aboutbox, image=aboutphoto)
  101.         l1.bm = aboutphoto
  102.         l1.pack()
  103.         ttk.Label(aboutbox, text='S计算器,一个S级轻量的计算器\n', font=('微软雅黑', 8)).pack()

  104.         ttk.Label(aboutbox,
  105.                   text='版本号:2.40\n' + '=' * 30 + '\n更新日志:\n'
  106.                   'a.优化整体UI,使用Windows7风格\n'
  107.                   'b.允许使用括号运算符\n'
  108.                   'c.在使用面板输入时,将在光标位置插入字符,而不是末尾'
  109.                   '\n' + '=' * 30 + '\n作者:ZiHeng',
  110.                   just=LEFT,
  111.                   font=('微软雅黑', 12)).pack(side=LEFT)

  112.     bt1 = ttk.Button(group1, text='关于', command=about)
  113.     bt1.grid(row=1, column=0, sticky='S')


  114. def esc(*args): root.quit()


  115. def sb3(event): menu.post(event.x_root, event.y_root)


  116. # 菜单函数定义结束

  117. # 定义运算函数

  118. def operation(a):
  119.     # 切片输入内容、去除末尾\n

  120.     global JieGuo
  121.     list1 = re.split(r'([*/+-])', a)
  122.     list2 = list1[-1].split('\n')
  123.     list1[-1] = list2[0]

  124.     # 乘除法函数
  125.     try:

  126.         while '/' in list1:
  127.             for index0, i in enumerate(list1):
  128.                 if i == '/':
  129.                     list1[index0] = '*'
  130.                     list1[index0 + 1] = 1 / float(list1[index0 + 1])

  131.         while '*' in list1:
  132.             for index0, i in enumerate(list1):
  133.                 if i == '*':
  134.                     list1[index0 - 1] = float(list1[index0 - 1]) * float(list1[index0 + 1])
  135.                     del list1[index0], list1[index0]

  136.         # 加减法函数

  137.         while '+' in list1:
  138.             list1.remove('+')
  139.         while '-' in list1:
  140.             for index0, i in enumerate(list1):
  141.                 if i == '-':
  142.                     list1[index0 + 1] = str(list1[index0]) + str(list1[index0 + 1])
  143.                     del list1[index0]
  144.         while len(list1) > 1:
  145.             list1[0] = float(list1[0]) + float(list1[1])
  146.             del list1[1]

  147.     except (ValueError, IndexError):
  148.         messagebox.showerror('错误', '输入字符无效')
  149.         inputc()
  150.     return str(list1[0])


  151. def qukuohao(a):
  152.     global JieGuo
  153.     list1 = re.split(r'([()])', a)
  154.     if list1.count('(') != list1.count(')'):
  155.         messagebox.showerror('错误', '反括号数量不等于正括号数量')
  156.         error = 1
  157.     while "(" in list1:
  158.         if error == 1:
  159.             return
  160.         count = 0
  161.         # 找到第一个反括号),向前找第一个(
  162.         for index, i in enumerate(list1):
  163.             if i == ')':
  164.                 count = index
  165.                 for m in range(count, -1, -1):
  166.                     if list1[m] == '(':
  167.                         # 将两个括号里面的元素赋值给一个新的列表,传入计算函数
  168.                         list2 = list1[m + 1:count]
  169.                         # 计算函数(list)返回值赋值给新的数插入到原列表
  170.                         kuohao = ''.join(list2)
  171.                         new_str = operation(kuohao)
  172.                         # 删除count,和m之间的元素
  173.                         list1.insert(m, new_str)
  174.                         for j in range(count-m+1):
  175.                             list1.pop(m+1)
  176.                         break
  177.     print(list1)
  178.     JieGuo = operation(''.join(list1))


  179. # 定义结束

  180. # 定义菜单

  181. menu = Menu(root, tearoff=False)

  182. Cmenu = Menu(menu, tearoff=False)  # 新建菜单,删除顶部虚线
  183. Cmenu.add_command(label='日志', command=lambda: messagebox.showinfo('请谅解', '功能制作中'))  # 将内容添加至下拉菜单
  184. Cmenu.add_separator()  # 新建分割线
  185. Cmenu.add_command(label='设置', command=settings)  # 将内容添加至菜单
  186. menu.add_cascade(label='菜单', menu=Cmenu)  # 实例化菜单

  187. Kmenu = Menu(menu, tearoff=False)  # 新建菜单,删除顶部虚线
  188. Kmenu.add_command(label='复制(Ctrl + C)', command=copy)  # 将内容添加至菜单
  189. Kmenu.add_command(label='粘贴(Ctrl + V)', command=pasta)  # 将内容添加至菜单
  190. Kmenu.add_separator()  # 新建分割线
  191. Kmenu.add_command(label='退出(Alt + F4)', command=esc)  # 将内容添加至菜单
  192. menu.add_cascade(label='快捷操作', menu=Kmenu)  # 实例化新建下单

  193. root.config(menu=menu)  # 实例化菜单

  194. root.bind('<Button-3>', sb3)  # 允许右键打开菜单

  195. # 菜单定义结束

  196. # 按钮定义区

  197. inputbox = Text(root, height=3, width=48, padx=5, pady=5)
  198. inputbox.grid(row=0, column=0, columnspan=4)

  199. eleft = ttk.Button(root,
  200.                    text='(',
  201.                    command=inputleft).grid(row=1, column=0)

  202. eright = ttk.Button(root,
  203.                     text=')',
  204.                     command=inputright).grid(row=1, column=1)

  205. ec = ttk.Button(root,
  206.                 text='C',
  207.                 command=inputc).grid(row=1, column=2)

  208. edel = ttk.Button(root,
  209.                   text='<--',
  210.                   command=inputdel).grid(row=1, column=3)

  211. e7 = ttk.Button(root,
  212.                 text='7',
  213.                 command=input7).grid(row=2, column=0)

  214. e8 = ttk.Button(root,
  215.                 text='8',
  216.                 command=input8).grid(row=2, column=1)

  217. e9 = ttk.Button(root,
  218.                 text='9',
  219.                 command=input9).grid(row=2, column=2)

  220. ediv = ttk.Button(root,
  221.                   text='/',
  222.                   command=inputdiv).grid(row=2, column=3)

  223. e4 = ttk.Button(root,
  224.                 text='4',
  225.                 command=input4).grid(row=3, column=0)

  226. e5 = ttk.Button(root,
  227.                 text='5',
  228.                 command=input5).grid(row=3, column=1)

  229. e6 = ttk.Button(root,
  230.                 text='6',
  231.                 command=input6).grid(row=3, column=2)

  232. etimes = ttk.Button(root,
  233.                     text='*',
  234.                     command=inputtimes).grid(row=3, column=3)

  235. e1 = ttk.Button(root,
  236.                 text='1',
  237.                 command=input1).grid(row=4, column=0)

  238. e2 = ttk.Button(root,
  239.                 text='2',
  240.                 command=input2).grid(row=4, column=1)

  241. e3 = ttk.Button(root,
  242.                 text='3',
  243.                 command=input3).grid(row=4, column=2)

  244. eminus = ttk.Button(root,
  245.                     text='-',
  246.                     command=inputminus).grid(row=4, column=3)

  247. edian = ttk.Button(root,
  248.                    text='.',
  249.                    command=inputdian).grid(row=5, column=0)

  250. e0 = ttk.Button(root,
  251.                 text='0',
  252.                 command=input0).grid(row=5, column=1)

  253. eok = ttk.Button(root,
  254.                  text='=',
  255.                  command=inputok).grid(row=5, column=2)

  256. eplus = ttk.Button(root,
  257.                    text='+',
  258.                    command=inputplus).grid(row=5, column=3)

  259. # 键盘操作————

  260. root.bind('<Return>', inputok)  # 回车键
  261. root.bind('<KeyPress-1>', input1)
  262. root.bind('<KeyPress-2>', input2)
  263. root.bind('<KeyPress-3>', input3)
  264. root.bind('<KeyPress-4>', input4)
  265. root.bind('<KeyPress-5>', input5)
  266. root.bind('<KeyPress-6>', input6)
  267. root.bind('<KeyPress-7>', input7)
  268. root.bind('<KeyPress-8>', input8)
  269. root.bind('<KeyPress-9>', input9)
  270. root.bind('<KeyPress-0>', input0)
  271. root.bind('<Delete>', inputc)  # DEL键
  272. root.bind('<BackSpace>', inputdel)  # 退格键
  273. root.bind('<Control-KeyPress-c>', copy)
  274. root.bind('<Control-KeyPress-C>', copy)
  275. root.bind('<Control-KeyPress-v>', pasta)
  276. root.bind('<Control-KeyPress-V>', pasta)

  277. # 禁止调整窗口大小

  278. root.resizable(width=False, height=False)

  279. # 进入窗口主事件循环

  280. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-7-30 16:25:46 | 显示全部楼层
支持楼主
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-4 20:57:04 | 显示全部楼层

老有激情阿,400行代码写个计算器
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-4 23:52:44 | 显示全部楼层
本帖最后由 XiaoPaiShen 于 2019-12-4 23:58 编辑

take a look
第一次按数字键不能输入?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-8 06:21:31 | 显示全部楼层
123
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-8 12:30:46 | 显示全部楼层
这么长啊。。。你写了多久啊 大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-9 08:26:12 From FishC Mobile | 显示全部楼层
厉害了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-10 16:31:04 | 显示全部楼层
学习下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-19 14:28:35 | 显示全部楼层
规划v记忆体
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-25 14:38:39 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-26 11:26:53 From FishC Mobile | 显示全部楼层
看看哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-1-6 10:41:48 | 显示全部楼层
tkinter用法都全忘了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-3 21:02:32 | 显示全部楼层
1111111111111
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-3 21:14:18 From FishC Mobile | 显示全部楼层
看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-4 16:48:29 | 显示全部楼层
pyperclip
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-13 00:19:10 | 显示全部楼层
看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-26 13:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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