| 輸入內(nèi)容 | 對(duì)應(yīng)按鍵 |
|---|---|
| 0~9 | 0~9 |
| + | + |
| - | - |
| * | * |
| / | / |
| 退格 | BackSpace |
| 清空 | Delete |
| 計(jì)算結(jié)果 | Return(Enter鍵) |
定義一個(gè)函數(shù) bind_print,跟 text_print 有點(diǎn)相似,但有些不一樣(原諒我技術(shù)差,不知道別的方法,只能重新定義一個(gè)函數(shù)):
def bind_print(event): #函數(shù)鍵盤事件輸入算式
global textchange,equal_is
if event.keysym!='Return':
if event.keysym=='BackSpace': #如果按鍵名等于“BackSpace”(退格鍵),那么就退格
a=str(textchange)[0:-1]
textchange=a
elif event.keysym=='Delete': #清空
textchange=''
else:
textchange=str(textchange)+str(event.char) #輸入按鍵內(nèi)容,char不會(huì)獲得Ctrl,Shift等特殊按鍵的文本
la.configure(text=textchange) #顯示內(nèi)容
show_is() #判斷是否錯(cuò)誤
equal_is=False
else:
text_equal()
如果按下的是特殊按鍵,除非是退格和回車,否則都不會(huì)有反應(yīng),
按下字母、數(shù)字、符號(hào)鍵的時(shí)候,輸入按鍵內(nèi)容。
接下來就是綁定鍵盤事件了:
root.bind('Key>',bind_print) #當(dāng)鍵盤按下任意鍵,執(zhí)行bind_print
這樣,界面布置和功能就完成了‘
將主窗體root放入主循環(huán)中:
root.mainloop()
import tkinter as tk
def create_btn(text,col,row,cs,rs,pri='',px=(1,1),py=(1,1)): #函數(shù)生成按鈕
if pri=='':
t=text
else:
t=pri
a=tk.Button(root,text=text,width=4,command=lambda:(text_print(t)))
a.grid(column=col,row=row,columnspan=cs,rowspan=rs,padx=px,pady=py,sticky='nswe')
return(a)
def grid_rowconfigure(*rows): #函數(shù)填充行
for i in rows:
root.grid_rowconfigure(i,weight=1)
def grid_columnconfigure(*cols): #函數(shù)填充列
for i in cols:
root.grid_columnconfigure(i,weight=1)
def bind_print(event): #函數(shù)鍵盤事件輸入算式
global textchange,equal_is
if event.keysym!='Return':
if event.keysym=='BackSpace':
a=str(textchange)[0:-1]
textchange=a
elif event.keysym=='Delete':
textchange=''
else:
textchange=str(textchange)+str(event.char)
la.configure(text=textchange)
show_is()
equal_is=False
else:
text_equal()
def text_print(x): #函數(shù)按鈕輸入算式
global textchange,equal_is
if x!='=':
if x=='←':
a=str(textchange)[0:-1]
textchange=a
elif x=='C':
textchange=''
else:
textchange=str(textchange)+str(x)
la.configure(text=textchange)
show_is()
equal_is=False
if x=='=':
text_equal()
def text_equal(event=None): #函數(shù)計(jì)算結(jié)果并上到輸入框
global textchange,equal_is
if lab['text']!='錯(cuò)誤' and equal_is==False:
textchange=lab['text']
la.configure(text=textchange)
lab.configure(text='')
equal_is=True
def show_is(): #顯示框內(nèi)容
global textchange
if textchange!='':
try:
textshow=eval(textchange)
except (SyntaxError,TypeError,NameError):
lab.configure(text='錯(cuò)誤')
else:
lab.configure(text=textshow)
else:
lab.configure(text='')
root=tk.Tk() #創(chuàng)建窗體
root.geometry('250x350')
root.title('計(jì)算器')
root.bind('Key>',bind_print)
equal_is=False #定義一些函數(shù)
textchange=''
la=tk.Label(root,text='',bg='white',fg='black',font=('宋體',24),anchor='w',relief='flat') #生成輸入框
la.grid(column=0,row=0,columnspan=5,rowspan=1,sticky='we')
lab=tk.Label(root,bg='white',fg='grey',height=1,font=('宋體',22),anchor='w',relief='flat') #生成顯示框
lab.grid(column=0,row=1,columnspan=5,rowspan=1,sticky='we')
btn={} #生成按鈕
btn['1']=create_btn('1',0,5,1,1)
btn['2']=create_btn('2',1,5,1,1)
btn['3']=create_btn('3',2,5,1,1)
btn['4']=create_btn('4',0,4,1,1)
btn['5']=create_btn('5',1,4,1,1)
btn['6']=create_btn('6',2,4,1,1)
btn['7']=create_btn('7',0,3,1,1)
btn['8']=create_btn('8',1,3,1,1)
btn['9']=create_btn('9',2,3,1,1)
btn['0']=create_btn('0',0,6,2,1)
btn['.']=create_btn('.',2,6,1,1)
btn['=']=create_btn('=',4,5,1,2)
btn['+']=create_btn('+',3,6,1,1)
btn['-']=create_btn('-',3,5,1,1)
btn['*']=create_btn('×',3,4,1,1,pri='*')
btn['/']=create_btn('÷',4,4,1,1,pri='/')
btn['←']=create_btn('←',1,2,1,1)
btn['C']=create_btn('C',2,2,1,1)
btn['(']=create_btn('(',3,2,1,1)
btn[')']=create_btn(')',4,2,1,1)
btn['**2']=create_btn('x²',3,3,1,1,pri='**2')
btn['**(-1)']=create_btn('1/x',4,3,1,1,pri='**(-1)')
grid_rowconfigure(2,3,4,5,6)
grid_columnconfigure(0,1,2,3,4)
root.mainloop()
以上就是做一個(gè)簡(jiǎn)單計(jì)算器的過程,效果如開頭所示。
本人技術(shù)還較差,歡迎向我提出任何的意見。
到此這篇關(guān)于如何利用python的tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)單的計(jì)算器的文章就介紹到這了,更多相關(guān)python tkinter簡(jiǎn)單計(jì)算器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:赤峰 陽泉 臨汾 日照 貴州 金華 雙鴨山 克拉瑪依
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《如何利用python的tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)單的計(jì)算器》,本文關(guān)鍵詞 如何,利用,python,的,tkinter,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。