给出代码:欢迎大家指正
#-------------------------------------------------------#
# coding:utf-8 #
# 功能:简单计算器 #
# 版本:1.0 #
# 时间:2016-6-17 #
# 作者:xingyys #
#-------------------------------------------------------#
from Tkinter import *
import math
# 计算器按键
key_list = ['7','8','9','+',
'4','5','6','-',
'1','2','3','*',
'0','.','%','/']
top = Tk()
top.geometry('320x280')
top.resizable(False, False) # 控制窗口大小
def button(root,text,row,col,command = None):
bt = Button(root,text = text,width = 10,height = 2,command = command)
bt.grid(row = row,column = col)
return bt
display = StringVar()
# 计算结果
def colc(event):
try:
display.set(eval(display.get()))
except:
display.set('ERROR!')
# 添加输出框
Label(top,textvariable = display,bg = 'white',anchor = E,width = 44,height = 2).grid(columnspan = 20)
# 添加按键
Key = enumerate(key_list)
for index,i in Key:
row = index / 4 + 1
col = index % 4
button(top,i,row,col,lambda d = display,s = i:d.set(d.get() + s))
# 添加特殊按键
Keys = enumerate(['CLR','^','√','='])
for x,y in Keys:
row = x / 4 + 5
col = x % 4
if(y == 'CLR'):
button(top,y,row,col,lambda d = display:d.set(''))
elif(y == '^'):
button(top,y,row,col,lambda d = display:d.set(d.get() + '**'))
elif(y == '√'):
button(top,y,row,col,lambda d = display:d.set((math.sqrt(float(d.get()))))) # 计算平方根
elif(y == '='):
bt = button(top,y,row,col)
bt.bind('',colc) # 释放时输出算术结果
top.mainloop()
#-------------------------------------------------------#
# coding:utf-8 #
# 功能:简单计算器 #
# 版本:1.0 #
# 时间:2016-6-17 #
# 作者:xingyys #
#-------------------------------------------------------#
from Tkinter import *
import math
# 计算器按键
key_list = ['7','8','9','+',
'4','5','6','-',
'1','2','3','*',
'0','.','%','/']
top = Tk()
top.geometry('320x280')
top.resizable(False, False) # 控制窗口大小
def button(root,text,row,col,command = None):
bt = Button(root,text = text,width = 10,height = 2,command = command)
bt.grid(row = row,column = col)
return bt
display = StringVar()
# 计算结果
def colc(event):
try:
display.set(eval(display.get()))
except:
display.set('ERROR!')
# 添加输出框
Label(top,textvariable = display,bg = 'white',anchor = E,width = 44,height = 2).grid(columnspan = 20)
# 添加按键
Key = enumerate(key_list)
for index,i in Key:
row = index / 4 + 1
col = index % 4
button(top,i,row,col,lambda d = display,s = i:d.set(d.get() + s))
# 添加特殊按键
Keys = enumerate(['CLR','^','√','='])
for x,y in Keys:
row = x / 4 + 5
col = x % 4
if(y == 'CLR'):
button(top,y,row,col,lambda d = display:d.set(''))
elif(y == '^'):
button(top,y,row,col,lambda d = display:d.set(d.get() + '**'))
elif(y == '√'):
button(top,y,row,col,lambda d = display:d.set((math.sqrt(float(d.get()))))) # 计算平方根
elif(y == '='):
bt = button(top,y,row,col)
bt.bind('',colc) # 释放时输出算术结果
top.mainloop()
编辑回复