Tkinter Entry and Text

Tkinter interface design input control Entry and text box control Text.

Table of contents

1. Place the controls

1. pack() function

2. place() function

3. grid() function

2. Simple controls

1. Entry input control

1.1 tk.StringVar() function: receive a string

1.2 tk.Entry() function: set an input control E

2. Text text box control

2.1 tk.Text() function: create a text box T

2.2 T.insert() function: text box insertion function

2.3 T.delete() function: text box clearing function

3. The complete program

1. Algorithm design

2. Running results


 

1. Place the controls

1. pack() function

E=tk.Entry(root,textvariable=var,font=('宋体',12),bg='white',fg='blue',width=20)
E.pack(side='left')
'''
E.pack(side='right)
E.pack(side='top')
E.pack(side='bottom'
'''

(1) side='left': center the input box on the left side of the current interface

(2) side='right': Center the input box on the right side of the current interface

(3) side='top': center the input box on the top of the current interface

(4) side='bottom': Center the input box at the bottom of the current interface

2. place() function

E=tk.Entry(root,textvariable=var,font=('宋体',12),bg='white',fg='blue',width=20)
E.place(x=100,y=100)

(1) Place the current interface at a distance of 100 from the far left of the interface and 100 from the top of the interface

(2) x represents the distance to the far left of the interface, and y represents the distance to the far right of the interface

3. grid() function

E=tk.Entry(root,textvariable=var,font=('宋体',12),bg='white',fg='blue',width=20)
for i in range(3):
    for j in range(3):
        E.grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10)

(1) Create a 3*3 grid with an input box inside

(2) row is the row, column is the column 

2. Simple controls

1. Entry input control

1.1 tk.StringVar() function: receive a string

var=tk.StringVar()

(1) Define var as a string 

1.2 tk.Entry() function: set an input control E

E=tk.Entry(root,textvariable=var,font=('宋体',12),bg='white',fg='blue',width=20)
E.pack()

(1) root indicates the interface where the control is located

(2) textvariable receives a dynamically changing string var

(3) font can set the font and font size

(4) bg is the background color of the input box

(5) fg is the color of the font

(6) width is the width of the input box

2. Text text box control

2.1 tk.Text() function: create a text box T

T=tk.Text(root,font=('宋体',12),width=20,height=3)
T.place(x=175,y=180)

(1) Create a text box on the root interface

(2) The font is Arial, the size is 12, the width of the text box is 20, and the height is 3

(3) Place the text box at 175 from the far left and 180 from the top

2.2 T.insert() function: text box insertion function

var2='Tkinter'
T.insert('end',var2)

(1) Insert a string in the text box

(2) 'end' means to continue inserting at the last position in the current text box

2.3 T.delete() function: text box clearing function

T.delete(1.0,tk.END)

(1) 1.0 means the first row, column 0, which is the beginning

(2) tk.END indicates the end of the text box

(3) This sentence can be understood as clearing the content of the text box

3. The complete program

1. Algorithm design

import tkinter as tk
root=tk.Tk()
root.title('Tkinter界面设计')
var1=tk.StringVar()
var2=tk.StringVar()
var1.set('Welcome')
var2='Tkinter'
E=tk.Entry(root,textvariable=var1,font=('宋体',12),bg='white',fg='blue',width=20)
E.place(x=175,y=80)
T=tk.Text(root,font=('宋体',12),width=20,height=3)
T.place(x=175,y=180)
T.insert('end',var2)
#T.delete(1.0,tk.END)
root.geometry('500x300')
root.mainloop()

2. Running results

0b94b82f8b6b420aaf6ed98ba20ceffa.png

 

Guess you like

Origin blog.csdn.net/m0_68111267/article/details/128700178