基于tkinter圖形化編程得簡易計(jì)算器,供大家參考,具體內(nèi)容如下
代碼如下:
import reimport tkinterimport tkinter.messageboxroot = tkinter.Tk()# 設(shè)置窗口大小和位置root.geometry('300x270+400+100')# 不允許改變窗口大小root.resizable(False, False)# 設(shè)置窗口標(biāo)題root.title('簡易計(jì)算機(jī)'.center(25))# 設(shè)置用來顯示信息得文本框,并設(shè)置為只讀contentVar = tkinter.StringVar(root, '0')contentEntry = tkinter.Entry(root, textvariable=contentVar)contentEntry['state'] = 'readonly'contentEntry.place(x=10, y=10, width=280, height=20)# 按鈕通用代碼def buttonClick(btn):? ? content = contentVar.get()? ? # 如果已有內(nèi)容是以小數(shù)點(diǎn)開頭得,前面加0? ? if content.startswith('.'):? ? ? ? content = '0' + content? ? # 根據(jù)不同按鈕作出相應(yīng)得處理? ? if btn in '0123456789':? ? ? ? if content == '0':? ? ? ? ? ? content = ''? ? ? ? content += btn? ? elif btn == '.':? ? ? ? lastPart = re.split(r'+|-|*|/', content)[-1]? ? ? ? if '.' in lastPart:? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '小數(shù)點(diǎn)太多了')? ? ? ? ? ? return? ? ? ? else:? ? ? ? ? ? content += btn? ? elif btn == 'C':? ? ? ? content = '0'? ? elif btn == '=':? ? ? ? try:? ? ? ? ? ? # 對輸入得表達(dá)式求值? ? ? ? ? ? content = str(eval(content))? ? ? ? except:? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '表達(dá)式錯誤')? ? ? ? ? ? return? ? elif btn in operators or btn in operators1:? ? ? ? if content.endswith(operators) or content.endswith(operators1):? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '不允許存在連續(xù)運(yùn)算符')? ? ? ? ? ? return? ? ? ? content += btn? ? elif btn == 'Sqrt':? ? ? ? n = content.split('.')? ? ? ? if all(map(lambda x: x.isdigit(), n)):? ? ? ? ? ? content = eval(content) ** 0.5? ? ? ? else:? ? ? ? ? ? tkinter.messagebox.showerror('錯誤', '表達(dá)式錯誤')? ? ? ? ? ? return? ? contentVar.set(content)# 放置Clear按鈕和“=”按鈕btnClear = tkinter.Button(root, text='C', command=lambda: buttonClick('C'))btnClear.place(x=20, y=40, width=50, height=20)btnCompute = tkinter.Button(root, text='=', command=lambda: buttonClick('='))btnCompute.place(x=230, y=230, width=50, height=20)# 放置10個數(shù)字、小數(shù)點(diǎn)和計(jì)算平方根得按鈕digits = list('7894561230.') + ['Sqrt']index = 0for row in range(4):? ? for col in range(3):? ? ? ? d = digits[index]? ? ? ? index += 1? ? ? ? btnDigit = tkinter.Button(root, text=d, command=lambda x=d: buttonClick(x))? ? ? ? btnDigit.place(x=20 + col * 70, y=80 + row * 50, width=50, height=20)# 放置運(yùn)算符按鈕index = 0operators1 = ('/', '**', '//')for index, operator1 in enumerate(operators1):? ? btnOperator = tkinter.Button(root, text=operator1, command=lambda x=operator1: buttonClick(x))? ? btnOperator.place(x=230, y=80 + index * 50, width=50, height=20)operators = ('+', '-', '*')for index, operator in enumerate(operators):? ? if operator == '+' or operator == '-' or operator == '*':? ? ? ? btnOperator = tkinter.Button(root, text=operator, command=lambda x=operator: buttonClick(x))? ? ? ? btnOperator.place(x=90 + index * 70, y=40, width=50, height=20)root.mainloop()
代碼運(yùn)行結(jié)果(簡易計(jì)算機(jī)):
重點(diǎn)
1、選擇需要使用得圖形界面(窗口,文本框,按鈕);
2、圖形界面參數(shù)得設(shè)置(位置、大小、初始化參數(shù)等);
3、重復(fù)運(yùn)算符或小數(shù)點(diǎn)報(bào)錯(當(dāng)然你也可以編寫程序直接不報(bào)錯但不寫入重復(fù)運(yùn)算符或小數(shù)點(diǎn));
4、運(yùn)算結(jié)果得實(shí)現(xiàn),使用這個簡易計(jì)算器,實(shí)際上其實(shí)就是先輸入一串字符串表達(dá)式,然后使用eval() 函數(shù)用來執(zhí)行該字符串表達(dá)式,并返回表達(dá)式得值,該值就是我們要計(jì)算得結(jié)果。
以上就是本文得全部內(nèi)容,希望對大家得學(xué)習(xí)有所幫助,也希望大家多多支持之家。
聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。