Today we will introduce the use of Button class in tkinter
Table of contents
Preface
In addition to pop-up windows, text boxes, and labels, the use of the Button class in tkinter is simply basic and basic. If you don’t know how to use buttons, you have to use Label and repeatedly perform mouse coordinate judgment. How terrible is that? The time complexity and CPU will directly strike! Having buttons is the icing on the cake, but not having them is worse... You can see how important buttons are
Today we will learn how to use Button from easy to difficult
study
1) Get to know Button
Button means that button is a commonly used control electrical component. It is a switch commonly used to connect or disconnect control circuits to control the operation of motors or other electrical equipment. But the button here refers to a component of varying shape and size within the window that can trigger events by clicking with the mouse.
Buttons are very common in life and have wide applicability, but if they are not designed properly, they can cause trouble. For example, the alarm is triggered by mistake, an accident occurs, etc. The same goes for buttons in programming. Improper design may affect the appearance of the page and cause bugs, or the computer may crash. So when setting the button, take out cephalosporins, isatis root, and a glass of water and be careful, and it will be ok...
In fact, many times we don’t need buttons. Button in tkinter.ttk, Checkbutton and Radiobutton in tkinter, dialog boxes in tkinter.simpledialog, and buttonbox in easygui can all replace Button, but Button is their grandma, so you have to learn it first. Grandma Button makes a bicycle again
2) Use Button
1) Call Button
As we all know, Button is a class in tkinter, so when calling, you only need tkinter.Button (parameter).
The syntax is as follows:
tkinter.Button(master=None, cnf={}, **kw)
Common **kw:
text: The text displayed on the button
command: event triggered when the button is clicked
height: the height of the button
width: the width of the button
bg: background color of button
fg: button font color
activebackground: the background color of the button when clicked
activeforeground: the font color of the button when clicked
font: font style, size
image: image of the button
Example:
import tkinter
a=tkinter.Tk()
def func():
print('我被触发了')
b=tkinter.Button(a,text='点击我',command=func,height=10,width=30,bg='red',fg='yellow',
activebackground='blue',activeforeground='red')
b.pack()
When not clicked When clicked
2) Button’s border
The border is very bright, everyone knows it. But how to add a border to Button?
If the answer is wrong , just use relief
FLAT: no border
GROOVE: small border
RAISED:Normal
RIDGE: slightly dented
SOLID: "Strong"
SUNKEN: sunken
Example:
from tkinter import *
def hello():
print('Hello!')
root=Tk()
button1=Button(root,text='click me!',command=hello,relief=FLAT)
button1.pack()
button2=Button(root,text='click me!',command=hello,relief=GROOVE)
button2.pack()
button3=Button(root,text='click me!',command=hello,relief=RAISED)
button3.pack()
button4=Button(root,text='click me!',command=hello,relief=RIDGE)
button4.pack()
button5=Button(root,text='click me!',command=hello,relief=SOLID)
button5.pack()
button6=Button(root,text='click me!',command=hello,relief=SUNKEN)
button6.pack()
root.mainloop()
3) Button status
Have you ever seen some software with buttons that are gray, cannot be pressed, and are locked? python can do it! !
All you need is the state bicycle wheel.
He has three states:
norma | active state |
active | Normal state |
disabled | locked status |
Example:
from tkinter import *
def hello():
print('Hello!')
def b2(event):
print(event,' is clicked.')
root=Tk()
for r in ['norma','active','disabled']:
Button(root,state=r,text=r).pack()
root.mainloop()
3) Example
Example:
import tkinter
a=tkinter.Tk()
l=tkinter.Label(a,text='你长大了要当什么')
l.grid(row=0,column=0)
b0=tkinter.Button(a,text='当太空人',bg='red',fg='green',relief=tkinter.SOLID)
b1=tkinter.Button(a,text='当喜羊羊',bg='blue',fg='red',relief=tkinter.RIDGE)
b2=tkinter.Button(a,text='当灰太狼',bg='green',fg='blue',relief=tkinter.FLAT)
b3=tkinter.Button(a,text='当人',bg='red',fg='green',state='disabled')
b0.grid(row=2,column=0)
b1.grid(row=2,column=2)
b2.grid(row=2,column=4)
b3.grid(row=2,column=6)
Summarize
This is the button. Anyone who is interested can check the information and tell me in the comment area what I haven’t mentioned.
Okay, this article ends here, viewers can go to bed~
Don’t spray unless you like it! !