#hello_again.py fromTkinterImport*classApp:def __init__(self,master): Frame=Frame (Master) frame.pack () Self.button=Button (frame, text="Close", fg="Blue", command=frame.quit)
#也可以用foreground代替fg (abbreviation) self.button.pack (Side=Left ) Self.hi_there=Button (frame, text="Hello", command=Self.say_hi) self.hi_there.pack (Side=Left )defSay_hi (self):Print("Hi,fudianheg. This is version 2 of the ' Hello World '") Root=Tk () app=App (Root) root.mainloop () #root. Destory () write the wrong word no wonder you can't destroy the window ...
Root.destroy ()
Operation Result:
When writing large program projects, it is best to wrap the code in classes.
When you click Hello, the console will display Example:
"Hi,fudianh eg. This is version 2 of the ' Hello World '
When you click Close, the program terminates.
__init__ creates a parent widget (master) that is created by (frame) and stored in a (frame) variable.
After creating this window, use the (Pack) function to display it.
A two button part is then created as a subassembly of the frame and displayed with the (pack) function.
We use a number of options in the constructor function of the part as the keyword parameter. The first button shows "off", and with blue as the foreground color, the second shows "Hello", and the command option is also used. This special command option specifies a function, or (in this case) a method of binding.
These two button parts are stored in the instance, they are close together, with the Side=left option, the left-hand arrangement, if the side option is not added, the default is top, the effect
Next, create a root, create a parent window with root as a parameter, and execute the Mainloop loop.
There are also Root.destroy () is optional, some environment in the program after the end of the exit, you need to add this. (such as the blogger's environment)
Postscript:
In fact, creating a widget does not necessarily need to be stored in a specific variable. If you do not need to manipulate it after creation, you can use this notation:
button (frame, text= "Hello", Command=self.hello). Pack (Side=left)
You can save it if you need to do it after you've created it, or for insurance purposes.
w = button (frame, text= "Hello", Command=self.hello)
W.pack (Side=left)
Postscript:
In TCL programming, if you want to create a button called "OK", as a child window of dialog, write this:
Button.dialog.ok
This is true in the Tkinter:
OK = Button (Dialog)
Tkinter will automatically assign a name to each window, the above OK and dialog just reference, not the real name, the real name is read out in Str (), if you want to determine the name of each widget, then you can:
OK = button (dialog, name= "OK")
Then the name of this window is called. Dialog.ok (If you forget dialog's name, it might be called. 1234323.ok), the widget will not change once the name is created.
Python Tkinter Learning Note (ii) Hello_again