Python interface Tkinter programming (basic control introduction and use)

1 Introduction

In the previous blog, the basic knowledge of Tkinter has been explained. In order to design your own program interface, you need to further understand the use of related controls. In this article, we will mainly introduce the following controls and their use, Label, Frame, Entry, Text, Button, Listbox, Scrollbar .

2. Control usage and introduction

The use of controls in Tkinter is based on the following forms: control type (root object, [property list]) , after the control is defined, it needs to be added to the main interface. The commonly used layout method is the pack() function. The control cannot be displayed on the interface without calling the pack() function.

2.1 Label

The Label control is the most common Tkinter control, mainly used for the display of label text. Usage: (parent can be understood as the parent window, the same below)

w = tk.Label(parent, option, ...)

option:

Attributes explain
bg Background color, 'white', 'black', 'red', 'green', 'blue', 'cyan', 'yellow', 'magenta' can be set
text This is the text you want to render on the label control
font The font attributes of the text to be displayed (color, size)
width Set the width of the control
height set control height
bitmap Set the image displayed in the Label control
anchor Controls the display position of the text in the Lable. When the size of the control is larger than the required size of the text, the default is tk.CENTER
padx Specifies the margin size in the left and right directions of the text, the default is 1
pads Specifies the size of the white space in the upper and lower directions of the text, the default is 1

Example:

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

import Tkinter

root_window = Tkinter.Tk()
root_window.title('Tkinter_Demo')
root_window.geometry('400x300')

hello_label = Tkinter.Label(root_window, text='hello world', bg='red', width=10, height=2)
hello_label.pack(side=Tkinter.TOP)  # 这里的side可以赋值为LEFT  RTGHT TOP  BOTTOM

root_window.mainloop()

Effect:
write picture description here

2.2 Frame

The Frame control is a Tkinter control used as a container for other controls. Instructions:

w = Frame(parent, option, ...)

option:

Attributes explain
bg or background The background color of the Frame control
cursor The style that needs to be displayed when the mouse is in the Frame control area
height The height of the Frame control
width The width of the Frame control

Example:

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

import Tkinter

root_window = Tkinter.Tk()
root_window.title('Tkinter_Demo')
root_window.geometry('400x300')

# main frame
main_frame = Tkinter.Frame(root_window)
main_label = Tkinter.Label(main_frame, text='MAIN FRAME')
main_label.pack()

# left frame
left_frame = Tkinter.Frame(main_frame)
left_label = Tkinter.Label(left_frame, text='LEFT FRAME')
left_label.pack()
left_frame.pack(side=Tkinter.LEFT)

# right frame
right_frame = Tkinter.Frame(main_frame)
right_label = Tkinter.Label(right_frame, text='RIGHT FRAME')
right_label.pack()
right_frame.pack(side=Tkinter.RIGHT)

main_frame.pack()

root_window.mainloop()

Effect:
write picture description here

2.3 Entry

The Entry control is used for text input in the Tkinter control. Instructions:

w = tk.Entry(parent, option, ...)

option:

Attributes explain
justify When the input text is smaller than the size of the text box, you can specify its position LEFT (default), CENTER, RIGHT
show When used as the input password box, you can set the non-plaintext to: show='*'
textvariable Associated with a StringVar class, you can use the set() and get() functions to set and get the value in the control
xscrollcommand Specify control slider events

Example:

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

import Tkinter

root_window = Tkinter.Tk()
root_window.title('Tkinter_Demo')
root_window.geometry('400x300')

m_str_var = Tkinter.StringVar()
m_entry = Tkinter.Entry(root_window, textvariable=m_str_var)
m_str_var.set('hello world')
m_entry.insert(Tkinter.END, ' nono')
m_entry.pack()

root_window.mainloop()

Effect:
write picture description here

2.4 Text

The Text control is used in Tkinter controls to display text. Instructions:

w = tk.Text(parent, option, ...)

option:

Attributes explain
undo Whether to enable the undo function, use False and True settings
maxundo maximum number of revocations

Insert operation: t.insert(mark, content)
Delete operation: t.delete(mark1, mark2)
where mark can be a line number, or a special identifier, such as
Tkinter.INSERT, Tkinter.CURRENT: corresponding to the current position of the mouse Character position
Tkinter.END: The last character of this Textbuffer Tkinter.SEL_FIRST: The
first character of the selected text field, if there is no selected area, an exception will be raised
Tkinter.SEL_LAST: The last character of the selected text field, if there is no selected area, then will throw an exception
Example :

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

import Tkinter

root_window = Tkinter.Tk()
root_window.title('Tkinter_Demo')
root_window.geometry('400x300')

m_text = Tkinter.Text(root_window)
m_text.insert(Tkinter.CURRENT, 'hello \n')
m_text.insert(Tkinter.END, 'world \n')
m_text.insert(Tkinter.END, 'nono')
m_text.pack()

root_window.mainloop()

Effect:
write picture description here

2.5 Button

Button controls are used as buttons in Tkinter controls. Instructions:

w = tk.Button(parent, option=value, ...)

option:

Attributes explain
image The picture displayed on the button control
text The text displayed on the button space
command Specify its callback function

Example:

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

import Tkinter

def button_clicked():
    m_text.insert(Tkinter.END, 'button clicked\n')


root_window = Tkinter.Tk()
root_window.title('Tkinter_Demo')
root_window.geometry('400x300')

m_text = Tkinter.Text(root_window)
m_text.pack()

m_button = Tkinter.Button(root_window, text='button', command=button_clicked)
m_button.pack()

root_window.mainloop()

Effect:
write picture description here

2.6 Listbox

The Listbox control is used as the list content display in the Tkinter control. Instructions:

w = tk.Listbox(parent, option, ...)

option:

Attributes explain
listvariable Used to set the value of the list box, or get the value of the list box, through the set() and get() functions
selectbackground Background color of the selected option
selectmode • tk.BROWSE: Default; • tk.SINGLE: Only one item can be selected, and the mouse cannot be dragged; • tk.MULTIPLE: Multiple rows are selected; • tk.EXTENDED: The current position is selected to the end

Example:

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

import Tkinter
import tkMessageBox

# 列表框项选中响应函数
def listbox_selected(event):
    info = m_list.get(m_list.curselection())
    tkMessageBox.showinfo('info', info)

root_window = Tkinter.Tk()
root_window.title('Tkinter_Demo')
root_window.geometry('400x300')

m_listbox_var = Tkinter.StringVar()
m_list = Tkinter.Listbox(root_window, listvariable=m_listbox_var)
temp_list = ['hello Miss1', 'hello Miss2', 'hello Miss3']
for item in temp_list:  # 插入元素
    m_list.insert(Tkinter.END, item)

m_list.delete(0, 1)  # 删除第一个元素
m_listbox_var.set(('hello Miss0', 'hello Miss2', 'hello Miss3'))  # 为列表框设置新值
m_list.bind('<ButtonRelease-1>', listbox_selected)  # 设置选中响应函数
m_list.pack()

root_window.mainloop()

Effect:
write picture description here

2.7 Scrollbar

Scrollbar controls are used as scroll bars in Tkinter controls. Instructions:

w = tk.Scrollbar(parent, option, ...)

option:

Attributes explain
command Scrollbar movement response function
orient Controls the position of the Scrollbar, which can be Tkinter.HORIZONTAL horizontal scrollbar, Tkinter.VERTICAL vertical

Example:

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

import Tkinter
import tkMessageBox

def listbox_selected(event):
    info = m_list.get(m_list.curselection())
    tkMessageBox.showinfo('info', info)

root_window = Tkinter.Tk()
root_window.title('Tkinter_Demo')
root_window.geometry('400x300')

m_listbox_var = Tkinter.StringVar()
m_list = Tkinter.Listbox(root_window, listvariable=m_listbox_var, selectbackground='red', selectmode=Tkinter.SINGLE)
temp_list = ['hello Miss1', 'hello Miss2', 'hello Miss3', 'hello Miss4', 'hello Miss5', 'hello Miss6',
             'hello Miss7', 'hello Miss8', 'hello Miss9', 'hello Miss10', 'hello Miss11', 'hello Miss12']
for item in temp_list:
    m_list.insert(Tkinter.END, item)

m_list.bind('<ButtonRelease-1>', listbox_selected)
m_list.pack()

m_scrl = Tkinter.Scrollbar(root_window)
m_scrl.pack(side=Tkinter.RIGHT, fill=Tkinter.Y)
m_list.configure(yscrollcommand=m_scrl.set)
m_list.pack()
m_scrl['command'] = m_list.yview

root_window.mainloop()

Effect:
write picture description here

3. Reference

  1. Tkinter 8.5 reference: a GUI for Python
  2. Tkinter instance learning of python

Guess you like

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