The @ Overview TK is a (available for multi-platform) GUI library; Tkinter is the interface of the Python language to access the library and the standard for Python's GUI development;
@ First GUI interface
From tkinter Import *
# Create a program window
= Tk ()
# Create a control instance
lable = Label (
window, # parent Container
text= Hello " # optional keyword parameters (see source note)
button = button (
window, # parent container
text=" Click Me " # Optional keyword parameters (see source comment) # to
package the control in the parent container
lable.pack ()
button.pack ()
# dead loop waiting for user action
Window.mainloop () Here to write a code piece
@ Handle Button Events
# Place the button in the window and set its Click event
# create Window
= Tk ()
# Create two instances
Btnok = button (
window, text= "OK", Foreground= "Red",
Command=dealbtnok # button Click event Listener, point to a response function name, do not add (), otherwise syntactically incorrect
)
btncancel = button (
window, text= "Cancel", background= "Red",
command=dealbtncancel
)
# to package the instance into the parent container
Btnok.pack ()
btncancel.pack ()
# Opens the message (dead) loop (waits for user input)
window.mainloop ()
Button Event Listener function
# Processing button click event
def dealbtnok ():
print ("Dealbtnok")
# Handle button click event
def dealbtncancel ():
print (" Dealbtncancel ")
@ Refactoring the above code in an object-oriented way
# Encapsulate window and button events as Class
Eventprocessbutton ():
# Add Windows and Event Listener
def __init__ (self) In initialization method: Window
= Tk ()
Btnok = button (window, text= "OK", foreground= "Red", Command=self.processbtnok)
btncancel = button (window, text= " Cancel ", background=" Red ", Command=self.processbtncancel)
btnok.pack ()
btncancel.pack
() Window.mainloop ()
Pass
# event listener is also encapsulated in the class
def Processbtnok (self):
print ("Processbtnok")
Pass
# Event Listening functions are also encapsulated in the class
def processbtncancel (self):
print ("Processbtncancel")
Pass
Create an instance of a class to implement a Window button event listener
Eventprocessbutton ()