Main use of Wxpython (most mature cross-platform Python GUI toolkit) foreplay: Veteran python GUI program (Tkinter)
Import Tkinter.messagebox asMessageBoxclassApplication (Frame): def __init__ (Self,master=None): frame.__init__ (SELF,MASTER,BG="Red") #设置框架类的父类 (based on master< main form >), frame can be considered as the parent container of the control Self.pack () #显示frame控件 self.createwidgets () def createwidgets (self): #用于创建控件 (f Rame's son) Self.nameinput=Entry (self) self.nameInput.pack () Self.alertbutton= Button (self,text="Hello", command=Self.hello) self.alertButton.pack () def hello (self): name= Self.nameinput.Get() Messagebox.showinfo ("Message","Hello,%s"%name) Root=Tk () root.title ("title") Root.wm_minsize ( $, $) App=application (Root) app.mainloop ()
def callback ():var.Set("hhhhhhh") Root=Tk ()var=Stringvar ()var.Set("66666") frame1=Frame (Root) frame2=Frame (root) lb= Label (frame1,textvariable=var, padx= -) Lb.pack (Side=Left ) # img= Image (file="1.gif", imgtype="Photo") img= Photoimage (file="1.gif") lb2= Label (frame1,image=img) lb2.pack (Side=Right ) BTNCM= Button (frame2,text="Next Step", command=callback) Btncm.pack () Frame1.pack () Frame2.pack () Root.mainloop ()
use of label labels (using pictures)
from tkinter import *== intvar () = Checkbutton (root,text="Test ", variable== Label (root,textvariable=v) l.pack () Root.mainloop ()
Checkbutton Use (1)
fromTkinter Import *Root=Tk () GIRLS= ["ASD",'DSA','FEF','Fwaf']v=[]def Change (): forIinchV:print (I.Get()) forGirlinchGirls:v.append (Intvar ()) b= Checkbutton (root,text=girl,variable=v[-1],command=Change ) #使用一个固有变量来记录状态 B.pack (anchor=W) #控件相对主窗口在左边root. Mainloop ()
Use of Checkbutton (2)
#对于单选框, multiple buttons correspond to only one variable, check box, multiple buttons for multiple values fromTkinter Import *def change (): Print (v.Get()) root=Tk () v=Intvar () Radiobutton (Root,text=" One", variable=v,value=1, Command=change). Pack (anchor=W) Radiobutton (Root,text=" Both", variable=v,value=2, Command=change). Pack (anchor=W) Radiobutton (Root,text="three", variable=v,value=3, Command=change). Pack (anchor=W) Root.mainloop ()
use of RadioButton (1)
fromTkinter Import *def change (): Print (v.Get()) root=Tk () v=Intvar () langes= [ ("python",1), ("JavaScript",2), ("Lua",3), ("Ruby",4)] forKey,valinchLanges:radiobutton (Root,text=key,variable=v,value=val,command=change). Pack (anchor=W) Root.mainloop ()
Use of RadioButton (2)
#对于单选框, multiple buttons correspond to only one variable, and for check boxes, multiple buttons correspond to multiple values (obtained using the list)
== Intvar () The variables we declare here should all be written after the main window is generated before you can
=='nonetype'object'_root'
1first enter the Intvar class.classIntvar (Variable): Def __init__ (self, master=none, Value=none, name=None): variable.__init__ (self, master, value, name)2. Enter the parent classclassvariable:def __init__ (self, master=none, Value=none, name=None): .....ifnot master: #看此处 (Master is the main window, it is a parameter, but we do not pass it in, so it is empty, enter the following code) Master=_default_root #_default_root是什么 self._root=master._root () self._tk=master.tk ...3. _default_root Find _support_default_root=1#也有用, look behind _default_root.=None #是一个全局变量, represents the main window but he is also empty, so there is a property error above, none _root () method------------------------------------------------------------------start viewing TK () root=Tk ()1. View Source CodeclassTk (Misc, Wm): Def __init__ (self, screenname=none, Basename=none, classname='Tk', USETK=1, sync=0, use=None): .....ifusetk: #这里默认传入1, enter the following logic SELF._LOADTK () ...2. View Self._loadtk () method def _loadtk (self): self._tkloaded=1 Global_default_root # Version Sanity checks ... # Create and register the Tkerror and exit commands # We need to inline parts of _register here, _ Register # would register differently-named Commands. ...... if_support_default_root and not _default_root: #查看上面的全局变量, find that you can enter the following logic code _default_root=Self #所以_default_root就是主窗口...------------------------------------------------------------------Conclusion: It can be known from the above that the use of variables such as Intvar requires _default_root (when we do not pass in master) and the main window generates root=TK (), the internal code is _default_root implemented. So both orders need to be guaranteed successively
Reason: source code Analysis
There are other kits pyqt and PYGTK are also widely used.
Python---Basics review (ix) graphical user interface