猫史档案馆


python计算器

用户:挽风不会甩狙挽风不会甩狙查看:0 回复:1 评论:0 创建时间:2022-04-17T07:58:03


#导入模块 import re import tkinter, tkinter.messagebox
#创建窗口 window = tkinter.Tk() window.geometry('300x270+400+100') window.resizable(False, False) window.title('计算器')
#定义按钮功能与不规范操作提醒 def ButtonOperation(m): theme = themeVar.get() if theme.startswith('.'): theme = '0'+theme if m in'0123456789': theme += m elif m == '.': Segmentationpart = re.split(r'\+|-|\*|/',theme)[-1] if'.'in Segmentationpart: tkinter.messagebox.showerror('错误', '重复出现的小数点') return else: theme += m elif m =='C': theme = '' elif m in operators: if theme.endswith(operators): thinter.messagebox.showerror('错误', '不允许存在连续运算符') return theme += m elif m == 'Sqrt': n = theme.split('.') ifall(map(lambda x:x.isdigit(), n)): theme = eval(theme)**0.5 else: tkinter.messagebox.showerror('错误', '算式错误') return elif m =='=': try: theme = str(eval(theme)) except: tkinter.messagebox.showerror('错误', '算式错误') return themeVar.set(theme)
#放置文本框 themeVar = tkinter.StringVar(window, '') themeEntry = tkinter.Entry(window, textvariable=themeVar) themeEntry['state'] = 'readonly' themeEntry.place(x=10, y=10, width=280, height=20)
#放置按钮 figure = list('0123456789.')+['Sqet'] index = 0 for row in range(4): for col inrange(3): a = figure[index] index += 1 m_figure = tkinter.Button(window, text=a, command=lambda x=a:ButtonOperation(x)) m_figure.place(x=20+col*70, y=80+row*50, width=50, heidgt=20) operators = ('+', '-', '*', '/', '**', '//') for index,operators in enumerate(operators): m_figure = tkinter.Button(window, text=operators, bg='or-ange', command=lambda x=operators:ButtonOperation(x)) m_operator.place(x=230, y=80+index*30, width=50, height=20) m_Clear = tkinter.Button(window, text='C', bg='red', command=lambda:ButtonOperation('C')) m_Clear.place(x=40, y=40, width=80,height=20) m_EqualSign = tkinter.Button(window, text='=', bg='yellow', command=lambda:ButtonOperation('=')) m_EqualSign.place(x=170, y=40, width=80, height=20)
window.mainloop()


回复

上一页1 页 / 共 1下一页
挽风不会甩狙挽风不会甩狙

#导入模块 import re import tkinter, tkinter.messagebox
#创建窗口 window = tkinter.Tk() window.geometry('300x270+400+100') window.resizable(False, False) window.title('计算器')
#定义按钮功能与不规范操作提醒 def ButtonOperation(m): theme = themeVar.get() if theme.startswith('.'): theme = '0'+theme if m in'0123456789': theme += m elif m == '.': Segmentationpart = re.split(r'\+|-|\*|/',theme)[-1] if'.'in Segmentationpart: tkinter.messagebox.showerror('错误', '重复出现的小数点') return else: theme += m elif m =='C': theme = '' elif m in operators: if theme.endswith(operators): thinter.messagebox.showerror('错误', '不允许存在连续运算符') return theme += m elif m == 'Sqrt': n = theme.split('.') ifall(map(lambda x:x.isdigit(), n)): theme = eval(theme)**0.5 else: tkinter.messagebox.showerror('错误', '算式错误') return elif m =='=': try: theme = str(eval(theme)) except: tkinter.messagebox.showerror('错误', '算式错误') return themeVar.set(theme)
#放置文本框 themeVar = tkinter.StringVar(window, '') themeEntry = tkinter.Entry(window, textvariable=themeVar) themeEntry['state'] = 'readonly' themeEntry.place(x=10, y=10, width=280, height=20)
#放置按钮 figure = list('0123456789.')+['Sqet'] index = 0 for row in range(4): for col inrange(3): a = figure[index] index += 1 m_figure = tkinter.Button(window, text=a, command=lambda x=a:ButtonOperation(x)) m_figure.place(x=20+col*70, y=80+row*50, width=50, heidgt=20) operators = ('+', '-', '*', '/', '**', '//') for index,operators in enumerate(operators): m_figure = tkinter.Button(window, text=operators, bg='or-ange', command=lambda x=operators:ButtonOperation(x)) m_operator.place(x=230, y=80+index*30, width=50, height=20) m_Clear = tkinter.Button(window, text='C', bg='red', command=lambda:ButtonOperation('C')) m_Clear.place(x=40, y=40, width=80,height=20) m_EqualSign = tkinter.Button(window, text='=', bg='yellow', command=lambda:ButtonOperation('=')) m_EqualSign.place(x=170, y=40, width=80, height=20)
window.mainloop()

点赞0


评论