Python: python的简单表达式计算器

这是一个简单的表达式计算器, 不太懂用Tkinter写GUI, 参考了别人的代码

 

from __future__ import division from Tkinter import Tk, Entry, Button, Label, mainloop from tkFont import Font def get_value (): v = '' try: v = eval(text.get()) #use eval to calculate the vlaue of the text except: pass if isinstance(v, (int, float, long)): pass else: v = 'Error...' label.config(text = v) #use config to change the text top = Tk() top.title("calculator") ft = Font(family = 'Courier New', size = 12) text = Entry(top, font = ft) button = Button(top, text = 'Ok', command = get_value) label = Label(text = '(+ - * / % **)', font = ft) Enter = lambda x: x.keycode == 13 and get_value() Key = lambda x: label.config(text = '(+ - * / % **)') text.bind('', Enter) #when the key is enter, execute the function Enter text.focus() text.bind('', Key) #when click the left-key, execute the function Key text.pack() button.pack() label.pack() mainloop()

你可能感兴趣的:(Python)