Python Tkinter GUI Programming Introduction

Source: Internet
Author: User
I. Introduction of Tkinter

Tkinter is a Python module, an interface called TCL/TK, which is a cross-platform scripting GUI interface. Tkinter is not the only Python graphics programming interface, but it is one of the more popular ones. The biggest feature is the cross-platform, the disadvantage is the performance is not good, the execution speed is slow.
The general method of using Tkinter is:
From Tkinter Import *
Or: Import Tkinter the difference between the two we talked about the module before.

Second, the use of Tkinter

First look at the development of GUI program, familiar with MFC friends should not be unfamiliar. In the GUI program, we will have a top-level window, which can include all the small window objects, such as tags, buttons, list boxes, and so on, which means we place other windows or controls in the top-level window. We can use the following statement to create a top-level window, or a root window:
Copy the Code code as follows:


Import Tkinter
top = tkinter.tk ()


(If the From Tkinter import * is used earlier, then TK () is enough)
Then we can set the "component" on this root window. Usually these components have some corresponding behaviors, such as mouse clicks, presses, and so on, which are called events, and the program responds to these times accordingly, called callbacks. This process becomes event driven.
After all creation and placement, immediately enter the main loop, the code is as follows:
Copy CodeThe code is as follows:


Tkinter.mainloop ()


TK has a lot of components, it is not possible to introduce each one, through a small example to see the use of one of the tags.
Copy CodeThe code is as follows:


>>> Import Tkinter
>>> top = tkinter.tk ()
>>> label = Tkinter.label (top,text= ' Hello World ')
>>> Label.pack ()
>>> Tkinter.mainloop ()


The result of the operation is

Here's an explanation:

The first line is the import module.
The second line creates the main window.
The third line, create a label label, it is a method with Tkinter label to achieve, the help of the label can be helpful.
The four-line, pack () is used to manage and display components, and its parameters we'll talk about later.
Line five, Mainloop () enters the main loop. The rest is on the system.

Here's a look at the configuration of the component. Each of the components in TK has a lot of option, which can change the appearance of the component, such as the display, color, size, position, event handler, etc.

For example: W=label (root,text= ' Hello ', fg= ' red ')

Create a W, the first parameter when his master widget is root, and all parameters have default. We can use the default to create, w.cget (option) to get an option value. You can also use W.config (option= ") to set the value of a parameter.

Third, the Geometry manager of Tkinter

People familiar with GUI programming know that it is cumbersome to put each component in place, not only to adjust its size, but also to
The relative position of the entire and other components. TK provides three managers to help us: Pack Grid Place
1. Pack
The pack is simple to use, which is w.pack (option). The common option is:
Side indicates which side to put the component on, Top (top), BOTTOM (bottom), left,right
PADX and Pady represent the reserved space for each edge and component of the parcel.
IPADX and Ipady, which represent the reserved space between each edge of the component and the content he contains.
Anchor represents the way the component is placed in parcel, by default, center.
2. Grid
The use method is similar to pack.
3. Place
The precise placement of a component is generally not very good.

The detailed usage and algorithms for this three can be consulted.

Here's a final example:

Look at the results first.

Change the size of text by dragging the progress bar

Check the code:
Copy the Code code as follows:


From Tkinter Import * #引入模块
#resize函数是用来改变文字大小的, called when the progress bar changes
def resize (ev=none):
Label.config (font= ' Helvetica-%d bold '% Scale.get ())
#config函数就是通过设置组件的参数来改变组件的, the font size is changed here
TOP=TK () #主窗口
Top.geometry (' 600x400 ') #设置了主窗口的初始大小600x400
Label=label (top,text= ' Hello world! ', font= ' Helvetica-12 bold ') #设置标签字体的初始大小
Label.pack (fill=y,expand=1)
#scale创建进度条, set
Scale=scale (Top,from_=10,to=40,orient=horizontal,command=resize)
Scale.set (#设置起始位置)
Scale.pack (fill=x,expand=1)
Quit = Button (top,text= ' QUIT ', command=top.quit,activeforeground= ' white ',
activebackground= ' Red ')
Quit.pack ()
Mainloop ()
  • 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.