8.2 Radio button control method
The following are common methods :
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
color=tk.StringVar()
color.set('red')
b1 = tk.Radiobutton(root,bg='red',text=' Red ',
variable=color,value='red')
b1.pack()
b2 = tk.Radiobutton(root,text=' Blue ',bg='blue',
variable=color,value='blue')
b2.pack()
b3 = tk.Radiobutton(root,text=' green ',bg='green',
variable=color,value='green')
b3.pack()
b2.select()
root.mainloop()
result :
8.2.2 deselect()
Clear the radio button selection . For example, at the beginning , yes ’ Red ’ The radio button for is selected , have access to deselect() Method , Cancel the selection . This is the time , No radio buttons are selected
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
color=tk.StringVar()
color.set('red')
b1 = tk.Radiobutton(root,bg='red',text=' Red ',
variable=color,value='red')
b1.pack()
b2 = tk.Radiobutton(root,text=' Blue ',bg='blue',
variable=color,value='blue')
b2.pack()
b3 = tk.Radiobutton(root,text=' green ',bg='green',
variable=color,value='green')
b3.pack()
b1.deselect()
root.mainloop()
result :
explain ; At this time 3 A radio button has no value selected , So it's a state of uncertainty . here color The value of the variable is empty , Although it is set at the beginning of the program color The value of is ‘ Red ’, because deselect() Method ,color The value of is cleared .
8.2.3 flash()
Blink the radio button several times between the active color and the normal color , But keep it as it started . You have to set activeforeground perhaps activebackground Any or all of , Otherwise it won't work .
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
color=tk.StringVar()
color.set('red')
b1 = tk.Radiobutton(root,bg='red',text=' Red ',activebackground='green',
activeforeground='yellow',variable=color,value='red')
b1.pack()
b2 = tk.Radiobutton(root,text=' Blue ',bg='blue',activebackground='red',
activeforeground='yellow',variable=color,value='blue')
b2.pack()
b3 = tk.Radiobutton(root,text=' green ',bg='green',activebackground='blue',
activeforeground='yellow',variable=color,value='green')
b3.pack()
def flash():
c=color.get()
if c== 'red':
b1.flash()
elif c=='blue':
b2.flash()
elif c=='green':
b3.flash()
else:
return
b5 = tk.Button(root,text='Flash',command=flash)
b5.pack()
root.mainloop()
8.2.4 invoke()
Simulate clicking the radio button . Call this function , You can select the corresponding radio button .
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
color=tk.StringVar()
color.set('red')
b1 = tk.Radiobutton(root,bg='red',text=' Red ',
variable=color,value='red')
b1.pack()
b2 = tk.Radiobutton(root,text=' Blue ',bg='blue',
variable=color,value='blue')
b2.pack()
b3 = tk.Radiobutton(root,text=' green ',bg='green',
variable=color,value='green')
b3.pack()
def invoke():
b3.invoke()
b5 = tk.Button(root,text='Invoke',command=invoke)
b5.pack()
root.mainloop()
result :
explain : The red button is selected at the beginning . Click on ’Invoke’ After button , system call invoke() Method , The set green button is selected .
I think the decision made two