| 輸入內容 | 對應按鍵 |
|---|---|
| 0~9 | 0~9 |
| + | + |
| - | - |
| * | * |
| / | / |
| 退格 | BackSpace |
| 清空 | Delete |
| 計算結果 | Return(Enter鍵) |
定義一個函數(shù) bind_print,跟 text_print 有點相似,但有些不一樣(原諒我技術差,不知道別的方法,只能重新定義一個函數(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) #輸入按鍵內容,char不會獲得Ctrl,Shift等特殊按鍵的文本
la.configure(text=textchange) #顯示內容
show_is() #判斷是否錯誤
equal_is=False
else:
text_equal()
如果按下的是特殊按鍵,除非是退格和回車,否則都不會有反應,
按下字母、數(shù)字、符號鍵的時候,輸入按鍵內容。
接下來就是綁定鍵盤事件了:
root.bind('Key>',bind_print) #當鍵盤按下任意鍵,執(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ù)計算結果并上到輸入框
global textchange,equal_is
if lab['text']!='錯誤' and equal_is==False:
textchange=lab['text']
la.configure(text=textchange)
lab.configure(text='')
equal_is=True
def show_is(): #顯示框內容
global textchange
if textchange!='':
try:
textshow=eval(textchange)
except (SyntaxError,TypeError,NameError):
lab.configure(text='錯誤')
else:
lab.configure(text=textshow)
else:
lab.configure(text='')
root=tk.Tk() #創(chuàng)建窗體
root.geometry('250x350')
root.title('計算器')
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()
以上就是做一個簡單計算器的過程,效果如開頭所示。
本人技術還較差,歡迎向我提出任何的意見。
到此這篇關于如何利用python的tkinter實現(xiàn)一個簡單的計算器的文章就介紹到這了,更多相關python tkinter簡單計算器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!