初学Python,tkinter计算器代
短信预约 -IT技能 免费直播动态提醒
import tkinter
root = tkinter.Tk()
root.minsize(185,250)
root.maxsize(185,250)
root.title('计算器')
rs = True
def huoqu(evt):
global rs
if label['text']=='0':
if evt.widget['text'] == 'C':
label['text'] = '0'
elif evt.widget['text']=='CE':
label['text']='0'
elif evt.widget['text']=='±':
label['text']='0'
elif evt.widget['text']=='←':
label['text']='0'
elif evt.widget['text'] in ['+','-','*','/','=']:
label['text']='0'
elif evt.widget['text']=='.':
label['text'] = label['text'] + evt.widget['text']
else:
label['text'] = evt.widget['text']
else:
if evt.widget['text'] == 'C':
label['text'] = '0'
elif evt.widget['text']=='CE':
if '+' in label['text']:
label_index = label['text'].find('+')
label['text'] = label['text'][:label_index+1]
elif '-' in label['text']:
label_index = label['text'].find('-')
label['text'] = label['text'][:label_index+1]
elif '*' in label['text']:
label_index = label['text'].find('*')
label['text'] = label['text'][:label_index+1]
elif '/' in label['text']:
label_index = label['text'].find('/')
label['text'] = label['text'][:label_index+1]
elif evt.widget['text']=='←':
label['text'] = label['text'][:-1]
print(label['text'])
if label['text']=='':
label['text'] = '0'
elif evt.widget['text']=='±':
if '+' in label['text']:
label['text']=label['text'].replace('+','-')
elif '-' in label['text']:
label['text']=label['text'].replace('-','+')
elif label['text'].count('.')==1 and evt.widget['text']=='.':
return
elif label['text'].count('+')==1 and evt.widget['text'] in ['+','-','*','/']:
label['text'] = label['text'].replace('+',evt.widget['text'])
elif label['text'].count('-')==1 and evt.widget['text'] in ['+','-','*','/']:
label['text'] = label['text'].replace('-',evt.widget['text'])
elif label['text'].count('*')==1 and evt.widget['text'] in ['+','-','*','/']:
label['text'] = label['text'].replace('*',evt.widget['text'])
elif label['text'].count('/')==1 and evt.widget['text'] in ['+','-','*','/']:
label['text'] = label['text'].replace('/',evt.widget['text'])
elif evt.widget['text'] == '=':
label['text'] = str(eval(label['text']))
rs = False
elif rs == True or evt.widget['text'] in ['+','-','*','/']:
label['text'] = label['text']+evt.widget['text']
rs = True
elif rs == False:
label['text'] = evt.widget['text']
rs = True
label = tkinter.Label(root,text='0', bg = '#ffffff',width=22,height=2,font=("黑体", 10, "bold"),anchor='e')
label.grid(row=0,column=0,columnspan = 4)
button_del = tkinter.Button(root,text = '←',width = 5,height =1,bg='#E0EEEE')
button_del.grid(row = 1,column = 0)
button_clear = tkinter.Button(root,text = 'C',width = 5,height =1,bg='#E0EEEE').grid(row = 1,column = 1)
button_fan = tkinter.Button(root,text = '±',width = 5,height =1,bg='#E0EEEE').grid(row = 1,column = 2)
button_ce = tkinter.Button(root,text = 'CE',width = 5,height =1,bg='#E0EEEE').grid(row = 1,column = 3)
button_1 = tkinter.Button(root,text = '1',width = 5,height =2,bg='#E0EEEE').grid(row = 2,column = 0)
button_2 = tkinter.Button(root,text = '2',width = 5,height =2,bg='#E0EEEE').grid(row = 2,column = 1)
button_3 = tkinter.Button(root,text = '3',width = 5,height =2,bg='#E0EEEE').grid(row = 2,column = 2)
button_jia = tkinter.Button(root,text = '+',width = 5,height =2,bg='#E0EEEE').grid(row = 2,column = 3)
button_4 = tkinter.Button(root,text = '4',width = 5,height =2,bg='#E0EEEE').grid(row = 3,column = 0)
button_5 = tkinter.Button(root,text = '5',width = 5,height =2,bg='#E0EEEE').grid(row = 3,column = 1)
button_6 = tkinter.Button(root,text = '6',width = 5,height =2,bg='#E0EEEE').grid(row = 3,column = 2)
button_jian = tkinter.Button(root,text = '-',width = 5,height =2,bg='#E0EEEE').grid(row = 3,column = 3)
button_7 = tkinter.Button(root,text = '7',width = 5,height =2,bg='#E0EEEE').grid(row = 4,column = 0)
button_8 = tkinter.Button(root,text = '8',width = 5,height =2,bg='#E0EEEE').grid(row = 4,column = 1)
button_9 = tkinter.Button(root,text = '9',width = 5,height =2,bg='#E0EEEE').grid(row = 4,column = 2)
button_cheng = tkinter.Button(root,text = '*',width = 5,height =2,bg='#E0EEEE').grid(row = 4,column = 3)
button_0 = tkinter.Button(root,text = '0',width = 5,height =2,bg='#E0EEEE').grid(row = 5,column = 0)
button_dian = tkinter.Button(root,text = '.',width = 5,height =2,bg='#E0EEEE').grid(row = 5,column = 1)
button_deng = tkinter.Button(root,text = '=',width = 5,height =2,bg='#E0EEEE').grid(row = 5,column = 2)
button_chu = tkinter.Button(root,text = '/',width = 5,height =2,bg='#E0EEEE').grid(row = 5,column = 3)
button_del.bind_all('<Button-1>',huoqu)
root.mainloop()
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341