Python Tkinter學習之計算機

來源:互聯網
上載者:User

標籤:python學習

初學Python tkinter,利用目前所學編寫一個簡單的計算機練習一下。

預期效果:

1.能夠實現加減乘除運算,

2.只能輸入數字

3.通過按鈕選擇進行哪種運算。

4.結果框不能修改,智能複製

5.按清除按鈕能夠清除所有內容

from  tkinter import *count = Tk()count.title("純醪八盅@計算機")def clear():    v1.set("")    v2.set("")    v3.set("")def calc():    print(v1.get(),v2.get())    print(v4.get())    if v4.get()=="+":        result = int(v1.get()) + int(v2.get())    elif v4.get()=="-":        result = int(v1.get()) - int(v2.get())    elif v4.get()=="x":        result = int(v1.get()) * int(v2.get())    else:        result = int(v1.get()) / int(v2.get())    v3.set(result)def test(content):    return content.isdigit()frame = Frame(count)frame.pack(padx=10,pady=10)v1=StringVar()v2=StringVar()v3=StringVar()testEnt = count.register(test)entry_1 = Entry(frame,width = 10,textvariable=v1,validate="key",validatecommand=(testEnt,"%P"))#entry_1.grid(row=0,column=0,padx=5,pady=5)v4=StringVar()v4.set("+")label = Label(frame,textvariable=v4).grid(row=0,column=1,padx=5,pady=5)entry_2 = Entry(frame,width = 10,textvariable=v2,validate="key",validatecommand=(testEnt,"%P"))entry_2.grid(row=0,column=2,padx=5,pady=5)label2 = Label(frame,text="=").grid(row=0,column=3,padx=5,pady=5)entry_3 = Entry(frame,width = 10,textvariable=v3,state ="readonly")#顯示結果的地區唯讀,不可修改entry_3.grid(row=0,column=4,padx=5,pady=5)def set1():    v4.set("+")def set2():    v4.set("-")def set3():    v4.set("x")def set4():    v4.set("/")button1 = Button(frame,text="+",command = set1).grid(row=1,column=1,padx=5,pady=5)button2 = Button(frame,text="-",command = set2).grid(row=1,column=2,padx=5,pady=5)button3 = Button(frame,text="x",command = set3).grid(row=1,column=3,padx=5,pady=5)button4 = Button(frame,text="/",command = set4).grid(row=1,column=4,padx=5,pady=5)button5 = Button(frame,text="清空",command = clear).grid(row=2,column=1,padx=5,pady=5)button6 = Button(frame,text="=",command=calc).grid(row=2,column=3,padx=5,pady=5)mainloop()

效果:

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M00/9D/F7/wKiom1mJZs7TDPiKAABYyzrtjhY495.png-wh_500x0-wm_3-wmp_4-s_4293504281.png" style="float:none;" title="1.png" alt="wKiom1mJZs7TDPiKAABYyzrtjhY495.png-wh_50" />

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M02/9D/F6/wKioL1mJZs6ibPCxAABZiuCSn0w482.png-wh_500x0-wm_3-wmp_4-s_3570605544.png" style="float:none;" title="2.png" alt="wKioL1mJZs6ibPCxAABZiuCSn0w482.png-wh_50" />

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M00/9D/F6/wKioL1mJZs-SLAINAABZk5mRCVI221.png-wh_500x0-wm_3-wmp_4-s_3510242539.png" style="float:none;" title="3.png" alt="wKioL1mJZs-SLAINAABZk5mRCVI221.png-wh_50" />

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M01/9D/F7/wKiom1mJZs-yPWM_AABZzEDtT_4622.png-wh_500x0-wm_3-wmp_4-s_1502517989.png" style="float:none;" title="4.png" alt="wKiom1mJZs-yPWM_AABZzEDtT_4622.png-wh_50" />

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M01/9D/F6/wKioL1mJZtDxoLbvAABXSWYUeIE736.png-wh_500x0-wm_3-wmp_4-s_1329858180.png" style="float:none;" title="5.png" alt="wKioL1mJZtDxoLbvAABXSWYUeIE736.png-wh_50" />


本文出自 “純醪八盅VSIT” 部落格,請務必保留此出處http://chunlaobazhong.blog.51cto.com/11499723/1954495

Python Tkinter學習之計算機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.