Mainly using wxPython (the most mature cross-platform python GUI toolkit)
Foreplay: Veteran python GUI program (Tkinter)
import tkinter.messagebox as messagebox class Application(Frame): def __init__(self,master=None): Frame.__init__(self,master,bg = " red " ) #Set the parent class of the frame class (based on master<main form> ), the frame can be regarded as the parent container of the control self.pack() #Display frame control self.createWidgets() def createWidgets(self): #Used to create widgets (children of frame) 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(200,200) 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=20) 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 " ,command= callback) btnCm.pack() frame1.pack() frame2.pack() root.mainloop ()
from tkinter import * root =Tk() v = IntVar() #selected is 1, unselected is 0 c = Checkbutton(root,text="Test",variable=v) c.pack() l = Label(root,textvariable=v) l.pack() root.mainloop ()
from tkinter import * root =Tk() GIRLS = ["asd",'dsa','fef','fwaf'] v = [] def change(): for i in v: print(i.get()) for girl in GIRLS: v.append(IntVar()) b = Checkbutton(root,text=girl,variable=v[- 1 ],command= change) #Use an inherent variable to record the status b.pack(anchor = W) #The control is on the left relative to the main window root.mainloop ()
#For radio boxes, multiple buttons correspond to only one variable, checkboxes, and multiple buttons correspond to multiple values from tkinter 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="two",variable=v,value=2,command=change).pack(anchor=W) Radiobutton(root,text="three",variable=v,value=3,command=change).pack(anchor=W) root.mainloop ()
from tkinter import * def change(): print(v.get()) root = Tk() v = IntVar () Langes = [ ("python",1), ("javascript",2), ("Lua",3), ("Ruby",4) ] for key,val in Langes: Radiobutton(root,text=key,variable=v,value=val,command=change).pack(anchor=W) root.mainloop ()
#For radio buttons, multiple buttons correspond to only one variable, and for checkboxes, multiple buttons correspond to multiple values (obtained using a list)
Notice: root = Tk() v = IntVar () All the variables we declare here should be written after the main window is generated.
Otherwise, when we write the variable before the main window is generated v = IntVar () root = Tk() will report an error AttributeError: 'NoneType' object has no attribute '_root'
1. First enter the IntVar class class IntVar(Variable): def __init__(self, master=None, value=None, name=None): Variable.__init__(self, master, value, name) 2. Enter the parent class class Variable: def __init__(self, master=None, value=None, name=None): ... if not master: #Look here (master is the main window, it is a parameter, but we did not pass it in when we used it, so it is empty, enter the following code) master = _default_root # what is _default_root self._root = master._root() self._tk = master.tk ... 3 ._default_root lookup _support_default_root = 1 #Also useful, see later _default_root = None # is a global variable representing the main window But he is also empty, so the above attribute error occurs, None has no _root() method ------------------------------------------------------------------ Start looking at Tk() root = Tk() 1 . View the source code class Tk(Misc, Wm): def __init__(self, screenName=None, baseName=None, className='Tk', useTk=1, sync=0, use=None): ... if useTk: #Here 1 is passed in by default, and the following logic is entered self._loadtk() ... 2. View the 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: #Check the above global variables and find that you can enter the following logic code _default_root = self #So _default_root is the main window ...... ------------------------------------------------------------------ Conclusion: From the above findings, we can know that: The use of variables such as IntVar requires _default_root (when we do not pass in master), and when the main window generates root =Tk(), the internal code implements _default_root. Therefore, the order of the two needs to be guaranteed.
There are other toolkits PyQT and PyGTK are also widely used.