9.2 How to select multiple buttons
The following are common methods :
9.2.1 select()
Set a multi selection button to the selected status , Can pass select() Specifies that a specific radio button is selected .
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Checkbutton(root,bg='red',text=' Red ',bd=5)
b1.pack()
b2 = tk.Checkbutton(root,text=' Blue ',bg='blue',bd=5)
b2.pack()
b3 = tk.Checkbutton(root,text=' green ',bg='green',bd=5)
b3.pack()
b2.select()
root.mainloop()
result :
9.2.2 deselect()
Follow select Method is the opposite operation , Cancel a radio button is selected .
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Checkbutton(root,bg='red',text=' Red ',bd=5)
b1.pack()
b2 = tk.Checkbutton(root,text=' Blue ',bg='blue',bd=5)
b2.pack()
b3 = tk.Checkbutton(root,text=' green ',bg='green',bd=5)
b3.pack()
def deselect():
b2.deselect()
b4=tk.Button(root,text=' Cancel blue ',command=deselect)
b4.pack()
root.mainloop()
result :
9.2.3 flash()
Blink the multi select button several times between the active state 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 . Note that only the selected button will work .
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
check=[tk.StringVar(),tk.StringVar(),tk.StringVar()]
for i in range(0,3):
check[i].set("0")
b1 = tk.Checkbutton(root,bg='red',text=' Red ',bd=5,
variable=check[0],activebackground='green',
activeforeground='yellow')
b1.pack()
b2 = tk.Checkbutton(root,text=' Blue ',bg='blue',bd=5,
variable=check[1],activebackground='red',
activeforeground='yellow')
b2.pack()
b3 = tk.Checkbutton(root,text=' green ',bg='green',bd=5,
variable=check[2],activebackground='blue',
activeforeground='yellow')
b3.pack()
def flash():
if check[0].get()=="1":
b1.flash()
if check[1].get()=="1":
b2.flash()
if check[2].get()=="1":
b3.flash()
b4=tk.Button(root,text='Flash',command=flash)
b4.pack()
root.mainloop()
9.2.4 invoke()
Simulate the situation when the multi selection button is selected .
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Checkbutton(root,bg='red',text=' Red ',bd=5)
b1.pack()
b2 = tk.Checkbutton(root,text=' Blue ',bg='blue',bd=5)
b2.pack()
b3 = tk.Checkbutton(root,text=' green ',bg='green',bd=5)
b3.pack()
def invoke():
b2.invoke()
b4=tk.Button(root,text='Invoke',command=invoke)
b4.pack()
root.mainloop()
result :
9.2.5 toggle()
Switch the status of multiple selection buttons . If it is currently selected , It becomes unselected . vice versa .toggle() The effect of invoke() It's the same .
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Checkbutton(root,bg='red',text=' Red ',bd=5)
b1.pack()
b2 = tk.Checkbutton(root,text=' Blue ',bg='blue',bd=5)
b2.pack()
b3 = tk.Checkbutton(root,text=' green ',bg='green',bd=5)
b3.pack()
def toggle():
b2.toggle()
b4=tk.Button(root,text='Toggle',command=toggle)
b4.pack()
root.mainloop()
result :