Beginner python tkinter, use the current learning to write a simple calculator to practice.
Expected effect:
1. Ability to implement subtraction operations,
2. Only numbers can be entered
3. Choose which operation to perform with the button.
4. Result box cannot be modified, smart copy
5. Press the Clear button to clear all content
FROM  TKINTER IMPORT *COUNT = TK () count.title ("Pure mash eight cup @ calculator") 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") #显示结果的区域只读, Non-modifiable 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= "Empty", 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 ()
Effect:
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 "/>
This article is from the "Pure mash Eight cup vsit" blog, please be sure to keep this source http://chunlaobazhong.blog.51cto.com/11499723/1954495
Python Tkinter Learning Calculator