Python Tkinter Learning (1)--The first Tkinter program

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.