import tkinter as tk
top=tk.Tk()
top.title('制作一个计算器')
shows=tk.Label(top,width=25,text='0',bg='yellow') #显示文本框,初值为0,背景为黄色
shows.grid(row=0,columnspan=4) #文本框在第0行,占用4格宽度
zero=tk.Button(top,text='0',width=5)
one=tk.Button(top,text='1',width=5)
two=tk.Button(top,text='2',width=5)
three=tk.Button(top,text='3',width=5)
four=tk.Button(top,text='4',width=5)
five=tk.Button(top,text='5',width=5)
six=tk.Button(top,text='6',width=5)
seven=tk.Button(top,text='7',width=5)
eight=tk.Button(top,text='8',width=5)
nine=tk.Button(top,text='9',width=5)
add=tk.Button(top,text='+',width=5)
sub=tk.Button(top,text='-',width=5)
mul=tk.Button(top,text='*',width=5)
div=tk.Button(top,text='/',width=5)
dot=tk.Button(top,text='.',width=5)
equal=tk.Button(top,text='=',width=5)
one.grid(row=1,column=0)
two.grid(row=1,column=1)
three.grid(row=1,column=2)
add.grid(row=1,column=3)
four.grid(row=2,column=0)
five.grid(row=2,column=1)
six.grid(row=2,column=2)
sub.grid(row=2,column=3)
seven.grid(row=3,column=0)
eight.grid(row=3,column=1)
nine.grid(row=3,column=2)
mul.grid(row=3,column=3)
zero.grid(row=4,column=0)
dot.grid(row=4,column=1)
equal.grid(row=4,column=2)
div.grid(row=4,column=3)
press_opt=False
def num_action(num): #在数字按钮事件被中被调用的函数,传入被点击的数字
global press_opt
if press_opt: #如果上一次点击了运算按钮
shows['text']=num #则显示框变为点击的数字
press_opt=False #将press_opt变为 False
else:
shows['text']=shows['text']+num #否则将前面点击的数字连在显示框数字后面
def zero_click(): #数字0点击函数,调用 num_action()函数,并传入字符0
num_action('0')
zero['command']=zero_click #设置数字0的按钮的command值为 zero_click函数
def one_click():
num_action('1')
one['command']=one_click
def two_click():
num_action('2')
two['command']=two_click
def three_click():
num_action('3')
three['command']=three_click
def four_click():
num_action('4')
four['command']=four_click
def five_click():
num_action('5')
five['command']=five_click
def six_click():
num_action('6')
six['command']=six_click
def seven_click():
num_action('7')
seven['command']=seven_click
def eight_click():
num_action('8')
eight['command']=eight_click
def nine_click():
num_action('9')
nine['command']=nine_click
has_dot=False #值为false的时候,显示框中的数字有小数点
def dot_click():
global has_dot
if not has_dot:
shows['text']=shows['text']+'.'
has_dot=True
dot['command']=dot_click
pre_opt='' #记录上一次点击的运算符
pre_num=0 #记录上一次点击的运算数
def compute():
global pre_opt
global pre_num
if pre_opt=='':
return
pre_num=eval(pre_num)
cur_num=eval(shows['text'])
if pre_opt=='+':
new_num=pre_num+cur_num
elif pre_opt=='-':
new_num = pre_num - cur_num
elif pre_opt=='*':
new_num = pre_num * cur_num
elif pre_opt=='/':
new_num = pre_num / cur_num
elif pre_opt=='=':
new_num =cur_num
shows['text']= '%s'%new_num
def opt_click(cur_opt): #传入当前点击的运算符
global pre_opt
global pre_num
global press_opt
global has_dot
compute()
pre_opt=cur_opt
pre_num=shows['text']
press_opt=True
has_dot=False
def add_click(): #加法按钮点击事件处理
opt_click('+')
add['command']=add_click
def sub_click():
opt_click('-')
sub['command']=sub_click
def mul_click():
opt_click('*')
mul['command']=mul_click
def div_click():
opt_click('/')
div['command']=div_click
def equal_click():
opt_click('=')
equal['command'] =equal_click
top.mainloop()
使用python制作简易计算器
最新推荐文章于 2024-06-20 10:40:26 发布