Tkinter's Label and Button

Tkinter is a built-in package of Python, which is mainly used for simple interface design and is very convenient to use.

Table of contents

1. Create interface

1. Specific steps

1.1 Import tkinter package

1.2 tk.Tk() function: Create a main interface and name it root

1.3 root.title() function: set a title for the root interface

1.4 root.geometry() function: set the size of the root interface 

1.5 root.mainloop() function: keep the root interface on the desktop

2. Complete program

2.1 Algorithm design

2.2 Running results

2. Simple controls 

1. Label control

1.1 tk.Label() function: create a label control L

1.2 L.pack() function: place label L

2. Button control

2.1 tk.Button() function: create a button B

2.2 B.pack() function: place button B

3. Complete procedure

3.1 Algorithm design

3.2 Running results


 

1. Create interface

1. Specific steps

1.1 Import tkinter package

import tkinter as tk

1.2 tk.Tk() function: Create a main interface and name it root

root=tk.Tk()

1.3 root.title() function: set a title for the root interface

root.title('Tkinter界面设计')

(1) Just enter a string directly 

1.4 root.geometry() function: set the size of the root interface 

root.geometry('500x300')

(1) 500 means the width of the interface, and 300 means the height of the interface, which can be set by yourself 

(2) Here '500x300' is a string, and x is the string 'x'

(3) Two parameters can also be set, one is the x-axis distance from the origin of the screen, and the other is the y-axis distance

1.5 root.mainloop() function: keep the root interface on the desktop

root.mainloop()

2. Complete program

2.1 Algorithm design

import tkinter as tk
root=tk.Tk()
root.title('Tkinter界面设计')
root.geometry('500x300')
root.mainloop()

2.2 Running results

ca94e3f1195f4903ab72fb0b626edc4a.png

2. Simple controls 

1. Label control

1.1 tk.Label() function: create a label control L

L=tk.Label(root,text='Welcome',font=('宋体',20),width=20,height=3,bg='white',fg='blue')

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

(2) text is the content of the Label tag

(3) font is the font and font size of the label content

(4) width is the width of the label

(5) height is the height of the label

(6) bg is the background color of the label

(7) fg is the font color of the label

1.2 L.place() function: place label L

L.pack()

(1) Center the L label on the top of the interface 

2. Button control

2.1 tk.Button() function: create a button B

B=tk.Label(root,text='确定',bg='blue',fg='white',width=5,height=2,command=None)

(1) The root, text, bg, fg, width, and height of the control are all common  

(2) command is the effect that will be triggered by clicking the button, followed by a custom function

2.2 B.pack() function: place button B

B.pack()

(1) The pack() function is common in controls

3. Complete procedure

3.1 Algorithm design

import tkinter as tk
root=tk.Tk()
root.title('Tkinter界面设计')
L=tk.Label(root,text='Welcome',font=('宋体',20),width=20,height=3,bg='white',fg='blue')
L.pack()
B=tk.Label(root,text='确定',bg='blue',fg='white',width=5,height=2,command=None)
B.pack()
root.geometry('500x300')
root.mainloop()

3.2 Running results

92d2e83eeacc43958e6fdc6357f85dfd.png

 

Guess you like

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