Tkintergui-First Knowledge tkinter

Source: Internet
Author: User
Preface

Recently there is a simple GUI program to write the idea, specifically what to say, but a requirement is to have cross-platform functions.

Specifically, I want this program to work with Windows and Linux at the same time, and to make it easy to change at any time.

For the programming language, for the purpose of the exercise, I chose Python, the version is 2.7, after all, 3 version of Personal

I feel a little unaccustomed.


Recommended Documents

Admittedly, there's not a lot of tkinter resources on document resources, but that's really enough for me.

Http://www.nmt.edu/tcc/help/pubs/tkinter.pdf

The document is in English, English is not good do not complain, early career change, anyway, I will teach their children must learn English.


Why Choose Tkinter

Python has a number of GUI modules to choose from, including the more popular and powerful modules such as Wxpython.

The reason I choose Tkinter is simple:

1. Tkinter is the Python installation package with the graphics library support package, no additional installation, convenient

2. Tkinter is lightweight GUI solution, lightweight disadvantage is not to do too complex interface, I need just to write a few buttons,

A few text boxes, a few check boxes such as simple GUI programs, this tkinter is fully capable of. The advantage of lightweight is simplicity.

It's important for me to write quickly what I want, and I don't want to write a GUI that needs to be studied for a long time ...

3. Python GUI editor idle is implemented using Tkinter, I think I write something that should not be more complicated than it

4. I have no harsh requirements for the appearance of the GUI, and Tkinter has a module called TTK that supports localized theme styles, making

GUI programs are not too ugly.


What's tkinter?

Tkinter is a module implemented by Python, the full name TK interface, which provides a library that uses Python to invoke TCL/TK to finally achieve the purpose of displaying a graphical interface.

TCL/TK itself is a GUI graphics library, similar to GTK and Qt.

In the Python manual, the call hierarchy for Tkinter is explained, as I understand it:

User's Python program-tkinter module-c implementation of the Tkinter extension-C/TCL language implementation of the TK plugin-xlib

Python itself is cross-platform, TCL/TK Graphics library is cross-platform, so tkinter can also cross platform ...

For Python programmers, it is not necessary to have a detailed understanding of the graphics library TCL/TK, of course, if you can understand that the Tkinter programming is a help group.

Just as Java programmers don't need to understand C + +, it's also helpful to know C + + for Java programmers.

My goal is "fast", so I don't get to know the TCK/TK, I'll take a look at the contents of the recommendation document to try it.


The simplest example of Tkinter

One of the simplest examples of this document is the Tkinter 8.5 reference:

#!/user/bin/env python
import tkinter as TK

class application (TK. Frame):
    def __init__ (self, master=none):
        tk. Frame.__init__ (self, Master)
        Self.grid ()
        self.createwidgets ()

    def createwidgets (self):
        Self.quitbutton = Tk. Button (self, text= ' Quit ', command=self.quit)
        Self.quitButton.grid ()

app = Application (
) App.master.title (' Sample application ')
App.mainloop ()
There is a description of each statement in the document, and here I am doing a translation work with a little bit of my own understanding.

Line 1th: This tells the shell program "What program to use to execute this script", which is basically supported by the scripting language in Linux (which is meaningless under Windows), and aside from its actual meaning, this is a comment.

Line 2nd: Import the namespace of the Tkinter module (that is, the class and symbols, and so on) and remap its namespace to TK (for a few words in programming) ... )

Line 4th: Here you define a class called application and indicate that this class inherits from the Tk.frame class, which means that Tk.frame supports the methods and members that application also support.

Line 5th: The __init__ function is a special method in class, similar to a constructor in C + +, which is usually called a constructor

Line 6th: Invoking the constructor of the parent class, which is usually a constructor of a subclass that calls the constructor of the parent class, which can be said to be a typical manifestation of inheritance

Line 7th: Here is a grid method called, which is needed to display the program, with a detailed description of the grid in the document, which may be further understood, and is not further deepened here.

Line 10th: A method is created for application that creates a button and sets the properties of the button buttons, such as: the displayed character is quit and the corresponding command function is Self.quit

Line 14th: This creates an instance of the application class, which runs __init__ this method when it is created, that is, the constructor

Line 15th: Call a method to set the title of the window to "Sample application"

Line 16th: Here begins the GUI program's message loop, is actually similar to MFC in the main loop, at this time the GUI program is in a dead loop waiting for various message events (such as mouse, keyboard and other messages)


The GUI interface displayed after program execution is as follows:


A little ugly. It doesn't matter, people can plastic surgery, the procedure is not yet beautify, I first focus on the inner beauty, the first function to achieve, there is time to do landscaping work, anyway this program is my own use.


Summary

After looking at the data, as well as simple learning, to understand what can tkinter is a thing, and the use of its built GUI program framework has a simple and intuitive understanding.

So far, Tkinter has given me the feeling that fresh and direct


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.