from __future__ import division
from Tkinter import Tk, Entry, Button, Label, mainloop
from tkFont import Font
def pp(ev=None):
foodl = ''
try: foodl = eval( text.get())
except : pass
if isinstance(foodl, (int,float,long)): pass
else: foodl = 'Error..'
label.config(text=foodl)
#主窗口
top = Tk()
top.title('compute')
ft = Font(family = ('Verdana'), size = 8 ) #字体
#注册组件
text = Entry(top, font= ft)
button = Button(top, text='计算(注意先后运算)', command=pp)
label = Label(text='运算符: + - * / % **', font=ft)
Enter = lambda x: x.keycode == 13 and pp()
Key = lambda x: label.config(text='运算符: + - * / % **')
text.bind('<Key>', Enter)#回车事件
text.focus() #获得焦点
#
text.bind('<Button-1>', Key)
text.pack()
button.pack()
label.pack()
mainloop()
计算器(python Tk编写)
最新推荐文章于 2024-10-27 20:00:57 发布
本文介绍了一个使用Python Tkinter库创建的简易计算器图形界面应用程序。该程序支持基本的数学运算,并通过简单的用户界面使用户能够输入表达式并显示计算结果。
4162

被折叠的 条评论
为什么被折叠?



