Python interface Tkinter programming (basic)

1. Introduction and preparation

Tkinter: The Tkinter module (Tk interface) is an interface to Python's standard Tk GUI toolkit. Tk and Tkinter can be used on most Unix platforms, as well as Windows and Macintosh systems. Subsequent versions of Tk8.0 can implement native window styles and run well on most platforms.
Can be installed using the pip management tool

pip install tkinter

2. Getting to know the interface

For friends who are accustomed to designing software interfaces under Windows, they can directly layout the interface in VS when designing the interface, and then VS can automatically add the corresponding classes to the project. But this is not the case in Tkinter. Relatively, the design in Tkinter is simpler and more convenient, such as the following example:

# -*- coding=utf-8 -*-

import Tkinter

root_window = Tkinter.Tk()  # 主窗口
root_window.title('Tkinter_Demo')  # 设置主窗口标题
root_window.geometry('400x300')  # 设置主窗口大小

# hello world 文本标签
hello_label = Tkinter.Label(root_window, text='hello world', bg='red', width=10, height=2)
hello_label.pack(side=Tkinter.TOP)

# 消息循环
root_window.mainloop()

Effect:
write picture description here

3. Controls in Tkinter

The controls in the interface provide a rich medium for the presentation of content. Tkinter provides controls such as buttons and labels. Mastering the use of these controls plays an important role in interface programming.

3.1 Controls in Tkinter

There are about 15 controls in Tkinter, which are listed here as shown in the following table

controls describe
Button Button control; displays the button in the program.
Canvas Canvas control; displays graphical elements such as lines or text
Checkbutton Multiple selection box control; used to provide multiple selection boxes in a program
Entry Input control; used to display simple text content
Frame Frame control; displays a rectangular area on the screen, mostly used as a container
Label Label control; can display text and bitmaps
Listbox Listbox control; the Listbox widget is used to display a list of strings to the user
Menubutton The menu button control, since the menu items are displayed.
Menu Menu control; displays menu bars, drop-down menus, and pop-up menus
Message Message control; used to display multi-line text, similar to label
Radiobutton Radio button control; displays the state of a radio button
Scale Range control; displays a numeric scale, a numeric interval that defines the range for the output
Scrollbar A scroll bar control, used when the content exceeds the visual area, such as a list box.
Text Text control; used to display multiple lines of text
Toplevel Container control; used to provide a single dialog, similar to Frame
Spinbox Input control; similar to Entry, but can specify input range values
PanedWindow PanedWindow is a window layout management plugin that can contain one or more child controls.
LabelFrame labelframe is a simple container control. Common and complex window layouts.
tkMessageBox A message box for displaying your application.

3.2 Standard properties

Standard properties are the common properties of all controls, such as size, font and color, and so on. These properties can be used to enrich the visual experience of the interface.

Attributes describe
Dimension control size;
Color control color;
Font control font;
Anchor anchor;
connects control style;
Bitmap bitmap;
Cursor cursor;

3.3 Control layout

Tkinter's controls support the following three control layout methods, pack, grid, place

geometric method describe
pack() Package;
grid() grid;
place() Location;

4. Reference

  1. Python GUI programming (Tkinter)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325767333&siteId=291194637