Introduction to Python Tkinter GUI programming _python

Source: Internet
Author: User
Tags pack

First, Tkinter Introduction

Tkinter is a Python module that is an interface to call TCL/TK, a Cross-platform scripting interface. Tkinter is not the only Python graphics programming interface, but it is one of the more popular. The biggest feature is Cross-platform, the disadvantage is that performance is not good, execution speed is slow.
The general way to use Tkinter is:
From Tkinter Import *
Or: Import Tkinter the difference between the two we talked about the module earlier.

second, the use of Tkinter

First look at the development of GUI programs, familiar with MFC friends should not be unfamiliar. In GUI programs, we have a top-level window that can include all of the widgets in the top-level window, like tags, buttons, list boxes, and so on, which means that we place other windows or controls in the top window. We can use the following statement to create a top-level window, or a root window:

Copy Code code as follows:

Import Tkinter
top = tkinter.tk ()

(if the previously used from tkinter import *, then TK () is enough)
Then we can set the "component" on this root window. Usually these components will have some corresponding behavior, such as mouse clicks, press, etc., these are called events, and the program will react according to these times, called callbacks. This process becomes an event-driven.
Once all the creation and placement is complete, enter the main loop immediately, with the following code:
Copy Code code as follows:

Tkinter.mainloop ()

There are a lot of TK components that are impossible to introduce, and a small example to see the use of one of the tags.
Copy Code code as follows:

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

The result of the operation is

The following explains:

The first line is the import module.
The second line creates the main window.
The third line creates a label label that is implemented by a method label with Tkinter, and help with the label.
Line four, the pack () is used to manage and display the component's parameters. We'll talk about it later.
Line five, Mainloop () into the main loop. The rest is on the system.

Here's a look at the configuration of the component. Each component in TK has a lot of option to change the appearance of the component by changing the option, such as display content, color, size, position, event handler function, and so on.

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

Create a W, the first parameter when his Master widget, is root, 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.

Three, Tkinter geometry Manager

People familiar with GUI programming know that it is cumbersome to put each component in place, not only to resize itself, but also to
The relative position of the integer and other components. TK provides three managers to help us: Pack Grid Place
1. Pack
Pack use is very simple, is w.pack (option). The commonly used option is:
Side indicates which side of the component to put, top (upper), BOTTOM (next), Left,right
PADX and Pady represent the reserved space for each side and component of the parcel.
IPADX and Ipady, which represent the space between each side of the component and the content he contains.
Anchor represents how the component is placed in parcel, and the default is center.
2, Grid
Use methods and packs like this.
3, place
The precise placement of a component is generally not used.

The detailed use and algorithm of this three can refer to the relevant data.

Here's a final example:

Look at the results first.

Change the text size by dragging the progress bar

Take a look at the code:

Copy Code code as follows:

From tkinter Import * #引入模块
#resize函数是用来改变文字大小的, called when the progress bar changes
def resize (ev=none):
Label.config (' Helvetica-%d bold '% Scale.get ())
#config函数就是通过设置组件的参数来改变组件的, the font size is changed here
TOP=TK () #主窗口
Top.geometry (' 600x400 ') #设置了主窗口的初始大小600x400
Label=label (top,text= ' Hello world! ', "Helvetica-12 bold") #设置标签字体的初始大小
Label.pack (fill=y,expand=1)
#scale创建进度条, setting
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.