|
发表于 2019-7-30 12:40:19
|
显示全部楼层
有漏洞,看看我的
- # -*- coding:utf-8 -*-
- """
- 此代码使用UTF-8编码
- 在Windows7上测试通过
- 作者ZiHeng
- QQ:3318448676
- 微信:b3318448676
- """
- from tkinter import *
- from tkinter import messagebox
- from tkinter import ttk
- import re
- import pyperclip
- JieGuo = ''
- root = Tk()
- root.title('计算器') # 设置窗口名字
- root.iconbitmap(r'calc.ico') # 设置窗口图标
- # 开始定义输入函数
- def input1(*args): inputbox.insert(INSERT, '1')
- def input2(*args): inputbox.insert(INSERT, '2')
- def input3(*args): inputbox.insert(INSERT, '3')
- def input4(*args): inputbox.insert(INSERT, '4')
- def input5(*args): inputbox.insert(INSERT, '5')
- def input6(*args): inputbox.insert(INSERT, '6')
- def input7(*args): inputbox.insert(INSERT, '7')
- def input8(*args): inputbox.insert(INSERT, '8')
- def input9(*args): inputbox.insert(INSERT, '9')
- def input0(*args): inputbox.insert(INSERT, '0')
- def inputplus(*args): inputbox.insert(INSERT, '+')
- def inputminus(*args): inputbox.insert(INSERT, '-')
- def inputtimes(*args): inputbox.insert(INSERT, '*')
- def inputdiv(*args): inputbox.insert(INSERT, '/')
- def inputleft(*args): inputbox.insert(INSERT, '(')
- def inputright(*args): inputbox.insert(INSERT, ')')
- def inputdian(*args): inputbox.insert(INSERT, '.')
- def inputc(*args): inputbox.delete(1.0, 'end')
- def inputdel(*args):
- beifen = inputbox.get(1.0, 'end')
- beifen = beifen[0:-2]
- inputbox.delete(1.0, 'end')
- inputbox.insert(END, beifen)
- del beifen
- def inputok(*args):
- number = inputbox.get('1.0', 'end')
- inputbox.delete('1.0', 'end')
- qukuohao(number)
- inputbox.insert(END, JieGuo)
- # 输入函数定义结束
- # 定义菜单函数
- def copy(*args):
- pyperclip.copy(inputbox.get(1.0, 'end'))
- messagebox.showinfo('提示', '复制成功!')
- def pasta(*args):
- messagebox.showinfo('提示', '粘贴成功!')
- inputbox.insert(END, pyperclip.paste())
- def settings():
- settingsbox = Toplevel(root)
- settingsbox.title('设置')
- settingsbox.resizable(width=False, height=False)
- group0 = LabelFrame(settingsbox, text='帮助', padx=5, pady=5)
- group0.pack(pady=10, padx=10, side=LEFT)
- label0 = Label(group0, text='这是一个功能待完善的计算器\n它还有许多功能没有添加'
- '\n也有许多Bug没有修复\n欢迎反馈漏洞(联系方式见关于)', jus=LEFT)
- label0.pack()
- def helpbtbox():
- helpForBt = Toplevel(settingsbox)
- helpForBt.title('快捷键操作帮助')
- # 帮助文字
- ttk.Label(helpForBt, text='<----0---->,输入0 | ', jus=LEFT).grid(row=0, column=0)
- ttk.Label(helpForBt, text='<----1---->,输入1 | ', jus=LEFT).grid(row=1, column=0)
- ttk.Label(helpForBt, text='<----2---->,输入2 | ', jus=LEFT).grid(row=2, column=0)
- ttk.Label(helpForBt, text='<----3---->,输入3 | ', jus=LEFT).grid(row=3, column=0)
- ttk.Label(helpForBt, text='<----4---->,输入4 | ', jus=LEFT).grid(row=4, column=0)
- ttk.Label(helpForBt, text='<----5---->,输入5 | ', jus=LEFT).grid(row=5, column=0)
- ttk.Label(helpForBt, text='<----6---->,输入6 | ', jus=LEFT).grid(row=6, column=0)
- ttk.Label(helpForBt, text='<----7---->,输入7 | ', jus=LEFT).grid(row=7, column=0)
- ttk.Label(helpForBt, text='<----8---->,输入8', jus=LEFT).grid(row=0, column=1)
- ttk.Label(helpForBt, text='<----9---->,输入9', jus=LEFT).grid(row=1, column=1)
- ttk.Label(helpForBt, text='<-退格键->,退格', jus=LEFT).grid(row=2, column=1)
- ttk.Label(helpForBt, text='<-Delete->,清空 ', jus=LEFT).grid(row=3, column=1)
- ttk.Label(helpForBt, text='<Ctrl + C>,复制', jus=LEFT).grid(row=4, column=1)
- ttk.Label(helpForBt, text='<Ctrl + V>,粘贴', jus=LEFT).grid(row=5, column=1)
- ttk.Label(helpForBt, text='<--回车-->,运算', jus=LEFT).grid(row=6, column=1)
- ttk.Label(helpForBt, text='<Alt + F4>,退出', jus=LEFT).grid(row=7, column=1)
- # 帮助文字结束
- # 禁止调整窗口大小
- helpForBt.resizable(width=False, height=False)
- # 完成
- ttk.Button(group0, text='快捷键操作帮助', command=helpbtbox).pack()
- group1 = LabelFrame(settingsbox, text='捐赠和关于', padx=5, pady=5)
- group1.pack(pady=10, padx=10)
- label1 = Label(group1, text='支付宝:Chinese.he.amber@gmail.com\n'
- '微信:b3318448676\nQQ:3318448676\n作者:ZiHeng', jus=LEFT)
- label1.grid(row=0, column=0, sticky='W')
- def about():
- aboutbox = Toplevel(settingsbox)
- aboutbox.title('关于')
- aboutphoto = PhotoImage(file='calc.gif')
- aboutbox.resizable(width=False, height=False)
- l1 = ttk.Label(aboutbox, image=aboutphoto)
- l1.bm = aboutphoto
- l1.pack()
- ttk.Label(aboutbox, text='S计算器,一个S级轻量的计算器\n', font=('微软雅黑', 8)).pack()
- ttk.Label(aboutbox,
- text='版本号:2.40\n' + '=' * 30 + '\n更新日志:\n'
- 'a.优化整体UI,使用Windows7风格\n'
- 'b.允许使用括号运算符\n'
- 'c.在使用面板输入时,将在光标位置插入字符,而不是末尾'
- '\n' + '=' * 30 + '\n作者:ZiHeng',
- just=LEFT,
- font=('微软雅黑', 12)).pack(side=LEFT)
- bt1 = ttk.Button(group1, text='关于', command=about)
- bt1.grid(row=1, column=0, sticky='S')
- def esc(*args): root.quit()
- def sb3(event): menu.post(event.x_root, event.y_root)
- # 菜单函数定义结束
- # 定义运算函数
- def operation(a):
- # 切片输入内容、去除末尾\n
- global JieGuo
- list1 = re.split(r'([*/+-])', a)
- list2 = list1[-1].split('\n')
- list1[-1] = list2[0]
- # 乘除法函数
- try:
- while '/' in list1:
- for index0, i in enumerate(list1):
- if i == '/':
- list1[index0] = '*'
- list1[index0 + 1] = 1 / float(list1[index0 + 1])
- while '*' in list1:
- for index0, i in enumerate(list1):
- if i == '*':
- list1[index0 - 1] = float(list1[index0 - 1]) * float(list1[index0 + 1])
- del list1[index0], list1[index0]
- # 加减法函数
- while '+' in list1:
- list1.remove('+')
- while '-' in list1:
- for index0, i in enumerate(list1):
- if i == '-':
- list1[index0 + 1] = str(list1[index0]) + str(list1[index0 + 1])
- del list1[index0]
- while len(list1) > 1:
- list1[0] = float(list1[0]) + float(list1[1])
- del list1[1]
- except (ValueError, IndexError):
- messagebox.showerror('错误', '输入字符无效')
- inputc()
- return str(list1[0])
- def qukuohao(a):
- global JieGuo
- list1 = re.split(r'([()])', a)
- if list1.count('(') != list1.count(')'):
- messagebox.showerror('错误', '反括号数量不等于正括号数量')
- error = 1
- while "(" in list1:
- if error == 1:
- return
- count = 0
- # 找到第一个反括号),向前找第一个(
- for index, i in enumerate(list1):
- if i == ')':
- count = index
- for m in range(count, -1, -1):
- if list1[m] == '(':
- # 将两个括号里面的元素赋值给一个新的列表,传入计算函数
- list2 = list1[m + 1:count]
- # 计算函数(list)返回值赋值给新的数插入到原列表
- kuohao = ''.join(list2)
- new_str = operation(kuohao)
- # 删除count,和m之间的元素
- list1.insert(m, new_str)
- for j in range(count-m+1):
- list1.pop(m+1)
- break
- print(list1)
- JieGuo = operation(''.join(list1))
- # 定义结束
- # 定义菜单
- menu = Menu(root, tearoff=False)
- Cmenu = Menu(menu, tearoff=False) # 新建菜单,删除顶部虚线
- Cmenu.add_command(label='日志', command=lambda: messagebox.showinfo('请谅解', '功能制作中')) # 将内容添加至下拉菜单
- Cmenu.add_separator() # 新建分割线
- Cmenu.add_command(label='设置', command=settings) # 将内容添加至菜单
- menu.add_cascade(label='菜单', menu=Cmenu) # 实例化菜单
- Kmenu = Menu(menu, tearoff=False) # 新建菜单,删除顶部虚线
- Kmenu.add_command(label='复制(Ctrl + C)', command=copy) # 将内容添加至菜单
- Kmenu.add_command(label='粘贴(Ctrl + V)', command=pasta) # 将内容添加至菜单
- Kmenu.add_separator() # 新建分割线
- Kmenu.add_command(label='退出(Alt + F4)', command=esc) # 将内容添加至菜单
- menu.add_cascade(label='快捷操作', menu=Kmenu) # 实例化新建下单
- root.config(menu=menu) # 实例化菜单
- root.bind('<Button-3>', sb3) # 允许右键打开菜单
- # 菜单定义结束
- # 按钮定义区
- inputbox = Text(root, height=3, width=48, padx=5, pady=5)
- inputbox.grid(row=0, column=0, columnspan=4)
- eleft = ttk.Button(root,
- text='(',
- command=inputleft).grid(row=1, column=0)
- eright = ttk.Button(root,
- text=')',
- command=inputright).grid(row=1, column=1)
- ec = ttk.Button(root,
- text='C',
- command=inputc).grid(row=1, column=2)
- edel = ttk.Button(root,
- text='<--',
- command=inputdel).grid(row=1, column=3)
- e7 = ttk.Button(root,
- text='7',
- command=input7).grid(row=2, column=0)
- e8 = ttk.Button(root,
- text='8',
- command=input8).grid(row=2, column=1)
- e9 = ttk.Button(root,
- text='9',
- command=input9).grid(row=2, column=2)
- ediv = ttk.Button(root,
- text='/',
- command=inputdiv).grid(row=2, column=3)
- e4 = ttk.Button(root,
- text='4',
- command=input4).grid(row=3, column=0)
- e5 = ttk.Button(root,
- text='5',
- command=input5).grid(row=3, column=1)
- e6 = ttk.Button(root,
- text='6',
- command=input6).grid(row=3, column=2)
- etimes = ttk.Button(root,
- text='*',
- command=inputtimes).grid(row=3, column=3)
- e1 = ttk.Button(root,
- text='1',
- command=input1).grid(row=4, column=0)
- e2 = ttk.Button(root,
- text='2',
- command=input2).grid(row=4, column=1)
- e3 = ttk.Button(root,
- text='3',
- command=input3).grid(row=4, column=2)
- eminus = ttk.Button(root,
- text='-',
- command=inputminus).grid(row=4, column=3)
- edian = ttk.Button(root,
- text='.',
- command=inputdian).grid(row=5, column=0)
- e0 = ttk.Button(root,
- text='0',
- command=input0).grid(row=5, column=1)
- eok = ttk.Button(root,
- text='=',
- command=inputok).grid(row=5, column=2)
- eplus = ttk.Button(root,
- text='+',
- command=inputplus).grid(row=5, column=3)
- # 键盘操作————
- root.bind('<Return>', inputok) # 回车键
- root.bind('<KeyPress-1>', input1)
- root.bind('<KeyPress-2>', input2)
- root.bind('<KeyPress-3>', input3)
- root.bind('<KeyPress-4>', input4)
- root.bind('<KeyPress-5>', input5)
- root.bind('<KeyPress-6>', input6)
- root.bind('<KeyPress-7>', input7)
- root.bind('<KeyPress-8>', input8)
- root.bind('<KeyPress-9>', input9)
- root.bind('<KeyPress-0>', input0)
- root.bind('<Delete>', inputc) # DEL键
- root.bind('<BackSpace>', inputdel) # 退格键
- root.bind('<Control-KeyPress-c>', copy)
- root.bind('<Control-KeyPress-C>', copy)
- root.bind('<Control-KeyPress-v>', pasta)
- root.bind('<Control-KeyPress-V>', pasta)
- # 禁止调整窗口大小
- root.resizable(width=False, height=False)
- # 进入窗口主事件循环
- mainloop()
复制代码 |
|