python使用tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
本文實(shí)例為大家分享了python使用tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
class Counter: #引入tkinter import tkinter as tk #引入消息彈窗模塊 import tkinter.messagebox as mbox #初始化Counter def __init__(self): #生成一個(gè)窗口對(duì)象 self.window = self.tk.Tk() #命名窗口對(duì)象的顯示title self.window.title('計(jì)算器') #設(shè)置窗口的大小 self.window.minsize(240, 325) self.window.maxsize(240, 325) #是否清空顯示框判定參數(shù) self.is_init_lable = False #設(shè)置菜單 self.set_menu() #設(shè)置顯示框 self.lable_show = self.tk.Label(text='', anchor='se', font=('黑體', 30), fg='black') self.lable_show.place(x=0, y=0, width=240, height=80) #設(shè)置按鈕組件 self.set_buttons() #將窗口放入主消息隊(duì)列 self.window.mainloop() #設(shè)置菜單 def set_menu(self): #創(chuàng)建總菜單 menubar = self.tk.Menu(self.window) #創(chuàng)建一個(gè)下拉菜單,并且加入文件菜單 filemenu = self.tk.Menu(menubar, tearoff=0) #創(chuàng)建下來(lái)菜單的選項(xiàng) filemenu.add_command(label="退出計(jì)算器", command=self.window.quit) #print author的函數(shù) def show_author(): self.mbox.showinfo(message='Wiz333@XDL 2017') filemenu.add_command(label="作者", command=show_author) #將文件菜單作為下拉菜單添加到總菜單中,并且將命名為操作 menubar.add_cascade(label="操作", menu=filemenu) #顯示總菜單 self.window.config(menu=menubar) #設(shè)置按鈕組件 def set_buttons(self): #7 btn7 = self.tk.Button(text='7', bd=2, font='黑體') btn7.place(x=0, y=90, width=60, height=40) #8 btn8 = self.tk.Button(text='8', bd=2, font='黑體') btn8.place(x=60, y=90, width=60, height=40) #9 btn9 = self.tk.Button(text='9', bd=2, font='黑體') btn9.place(x=120, y=90, width=60, height=40) #+ btn_jia = self.tk.Button(text='+', bd=2, font='黑體') btn_jia.place(x=180, y=90, width=60, height=40) #4 btn4 = self.tk.Button(text='4', bd=2, font='黑體') btn4.place(x=0, y=130, width=60, height=40) #5 btn5 = self.tk.Button(text='5', bd=2, font='黑體') btn5.place(x=60, y=130, width=60, height=40) #6 btn6 = self.tk.Button(text='6', bd=2, font='黑體') btn6.place(x=120, y=130, width=60, height=40) #- btn_jian = self.tk.Button(text='-', bd=2, font='黑體') btn_jian.place(x=180, y=130, width=60, height=40) #1 btn1 = self.tk.Button(text='1', bd=2, font='黑體') btn1.place(x=0, y=170, width=60, height=40) #2 btn2 = self.tk.Button(text='2', bd=2, font='黑體') btn2.place(x=60, y=170, width=60, height=40) #3 btn3 = self.tk.Button(text='3', bd=2, font='黑體') btn3.place(x=120, y=170, width=60, height=40) #* btn_cheng = self.tk.Button(text='*', bd=2, font='黑體') btn_cheng.place(x=180, y=170, width=60, height=40) #0 btn0 = self.tk.Button(text='0', bd=2, font='黑體') btn0.place(x=0, y=210, width=120, height=40) #. btn_point = self.tk.Button(text='.', bd=2, font='黑體') btn_point.place(x=120, y=210, width=60, height=40) #/ btn_chu = self.tk.Button(text='/', bd=2, font='黑體') btn_chu.place(x=180, y=210, width=60, height=40) #取消 btn_cancel = self.tk.Button(text='C', bd=2, font='黑體') btn_cancel.place(x=0, y=250, width=60, height=40) #確定 btn_ok = self.tk.Button(text='=', bd=2, font='黑體') btn_ok.place(x=60, y=250, width=180, height=40) #綁定Button的點(diǎn)擊事件 btn7.bind_class('Button', '<Button-1>', self.click_button) #綁定Button的點(diǎn)擊事件 def click_button(self,e): #判斷是否是新的運(yùn)算,如果是則清空顯示框 if self.is_init_lable: self.lable_show['text'] = '' self.is_init_lable = False #label_show顯示的累加 font = e.widget['text'] self.lable_show['text'] += font #異常捕獲 try: #判定運(yùn)算符號(hào)重復(fù)的時(shí)候,使用最后輸入的符號(hào) if self.lable_show['text'][-1] in ['+','-','*','/'] and self.lable_show['text'][-2] in ['+','-','*','/']: header = self.lable_show['text'][:-2] footer = self.lable_show['text'][-1] self.lable_show['text'] = header+footer except: pass #普通計(jì)算 if e.widget['text'] == '=': try: res = eval(self.lable_show['text'][:-1]) #print(res) #小數(shù)點(diǎn)取到9位 self.lable_show['text'] = str(round(float(res), 5)) self.isinit = True except ZeroDivisionError: #除法時(shí),除數(shù)不能為0 self.mbox.showerror(message='除法計(jì)算時(shí)!除數(shù)不能為0!') except: self.mbox.showerror(message='算式有誤') #取消當(dāng)前輸入的字符 if e.widget['text'] == 'C': cancel_res = self.lable_show['text'][:-2] self.lable_show['text'] = cancel_res #實(shí)例化計(jì)算器對(duì)象 wiz = Counter()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python?tkinter實(shí)現(xiàn)計(jì)算器功能
- 如何利用python的tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)單的計(jì)算器
- Python tkinter實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器代碼實(shí)例
- Python+tkinter使用40行代碼實(shí)現(xiàn)計(jì)算器功能
- Python Tkinter實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- Python+tkinter使用80行代碼實(shí)現(xiàn)一個(gè)計(jì)算器實(shí)例
- 利用Tkinter(python3.6)實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器
- Python編程使用tkinter模塊實(shí)現(xiàn)計(jì)算器軟件完整代碼示例
- 基于python的Tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)易計(jì)算器
- python tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
相關(guān)文章
基于Python的ModbusTCP客戶端實(shí)現(xiàn)詳解
這篇文章主要介紹了基于Python的ModbusTCP客戶端實(shí)現(xiàn)詳解,Modbus Poll和Modbus Slave是兩款非常流行的Modbus設(shè)備仿真軟件,支持Modbus RTU/ASCII和Modbus TCP/IP協(xié)議 ,經(jīng)常用于測(cè)試和調(diào)試Modbus設(shè)備,觀察Modbus通信過(guò)程中的各種報(bào)文,需要的朋友可以參考下2019-07-07Python灰度變換中的對(duì)數(shù)變換專項(xiàng)分析實(shí)現(xiàn)
灰度變換是指根據(jù)某種目標(biāo)條件按一定變換關(guān)系逐點(diǎn)改變?cè)磮D像中每個(gè)像素灰度值的方法。目的是改善畫質(zhì),使圖像顯示效果更加清晰。圖像的灰度變換處理是圖像增強(qiáng)處理技術(shù)中的一種非常基礎(chǔ)、直接的空間域圖像處理方法,也是圖像數(shù)字化軟件和圖像顯示軟件的一個(gè)重要組成部分2022-10-10python模塊smtplib實(shí)現(xiàn)純文本郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了python模塊smtplib實(shí)現(xiàn)純文本郵件發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Pygame實(shí)戰(zhàn)之經(jīng)典泡泡龍小游戲
Python版的消除類的游戲還是很多的,今天就出一個(gè)消除類——泡泡龍小游戲。文中的示例代碼很詳細(xì),感興趣的小伙伴快來(lái)跟隨小編一起學(xué)習(xí)一下吧2021-12-12Python實(shí)現(xiàn)正弦信號(hào)的時(shí)域波形和頻譜圖示例【基于matplotlib】
這篇文章主要介紹了Python實(shí)現(xiàn)正弦信號(hào)的時(shí)域波形和頻譜圖,涉及Python數(shù)學(xué)運(yùn)算與圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2018-05-05python簡(jiǎn)單實(shí)現(xiàn)獲取當(dāng)前時(shí)間
最近項(xiàng)目中經(jīng)常需要python去取當(dāng)前的時(shí)間,雖然不是很難,但是老是忘記,用一次丟一次,為了能夠更好的記住,我今天特意寫下python 當(dāng)前時(shí)間這篇文章,如果你覺(jué)的對(duì)你有用的話,可以收藏下。2016-08-08