Note: This article can be reproduced, reproduced please indicate the source: http://www.cnblogs.com/collectionne/p/6885066.html.
Tkinter Introduction
Python supports multiple graphics libraries, such as QT, WxWidgets, and so on. But Python's standard GUI library is tkinter. Tkinter is the abbreviation for TK interface. Python provides the Tkinter package, which contains the Tkinter interface.
Start Writing Programs
In this section, we will write a tkinter program with only one quit button.
To use Tkinter, you need to import the Tkinter package First:
Import Tkinter as TK
The import statement means that the Tkinter package was imported, but an alias tkwas defined for Tkinter . We can access the Tkinter directly with tk.xxx in the back . XXX has.
Then, we need to derive a application class from the tkinter Frame class:
Class Application (TK. Frame):
Then we define a constructor for the application class __init__ ():
def __init__ (self, master=none): tk. Frame.__init__ (self, Master) Self.grid () self.createwidgets ()
The first sentence Tk. Frame.__init__ (self, master), this statement invokes the constructor of the parent class frame of application and initializes the frame.
The second sentence Self.grid (), usually a window is not displayed by default, the grid () method allows the window to be displayed on the screen.
The third sentence, self.createwidgets (), invokes the createwidgets () method defined later.
Next is the createwidgets () function:
def createwidgets (self): Self.quitbutton = tk. Button (self, text= ' Quit ', command=self.quit) Self.quitButton.grid ()
The first line Self.quitbutton = tk. button (self, text= ' Quit ', command=self.quit), this Quitbutton is a property created by itself, creating a buttons that are labeled Quit and then exit when clicked.
The second line Self.quitButton.grid (), like a button and window, is not displayed by default, and the grid () method is called to make the button appear on the window.
Then there was the execution:
App = Application () App.master.title = ' Hello Tkinter ' App.mainloop ()
The first line of app = Application ()creates a application object.
The second line, app.master.title = ' Hello Tkinter ', sets the window caption to "Hello Tkinter".
The third line App.mainloop ()starts the program main loop.
Python Tkinter Learning (1)--The first Tkinter program