How do I set all the check box to true by using button function in selectAllRating? Below is my code Please advise
for i in range(5):
option = StringVar(value="")
starCheckBox.append(option)
Checkbutton(master, text='5 Stars',variable=starCheckBox[0], onvalue="5", offvalue="").place(x=0,y=20)
Checkbutton(master, text='4 Stars',variable=starCheckBox[1], onvalue="4", offvalue="").place(x=100,y=20)
Checkbutton(master, text='3 Stars',variable=starCheckBox[2], onvalue="3", offvalue="").place(x=200,y=20)
Checkbutton(master, text='2 Stars',variable=starCheckBox[3], onvalue="2", offvalue="").place(x=300,y=20)
Checkbutton(master, text='1 Stars',variable=starCheckBox[4], onvalue="1", offvalue="").place(x=400,y=20)
Button(master, text='Select all Hotel(s)', command=selectAllHotel).place(x=0,y=190)
def selectAllRating():
for i in starCheckBox:
i.set('1')
CodePudding user response:
The Checkbutton has a method to select it, it is called select
and also has a deselect
method for the opposite.
def selectAllRating():
for i in starCheckBox:
i.select()
If you want just to set the Checkbutton in a different state as it was before, you could use toggle
.
If you want to toggle
and run the associated command
you can use invoke
. All of these methods will be reflected by the associated variable
.
See the documentation for more inforamtion
Edit:
StringVar not able to do so?
Yes it is, but you need to set the associated variable
to the onvalue
.
In addition, each checkbutton monitors its associated variable and automatically selects and deselects itself when the variables value changes to and from the button's “on”, “off” and “tristate” values.
So your code would need to look like this:
def selectAllRating():
for i in range(1, len(var)):
var[i].set(i)
Full example:
import tkinter as tk
root = tk.Tk()
var = []
for i in range(1, 5 1):
v = tk.StringVar(value='')
var.append(v)
b = tk.Checkbutton(
root, text=f'{i} Stars', variable = v, onvalue = i)
b.pack()
for i in range(len(var)):
print(i)
var[i].set(i 1)
root.mainloop()
CodePudding user response:
You can do this in a loop more easily if you name the checkbutton
widgets. Then you can call invoke
on each to activate them. Note that you'll have to place
each button separately because place()
doesn't return a value, and otherwise the variables like cb_a
won't respond to invoke
(you'll get an error). You can also place
them using a for loop, so I've done that here
cb_a = Checkbutton(
master,
text='5 Stars',
variable=starCheckBox[0],
onvalue="5",
offvalue=""
) # note that you *don't* call `place` here any more
# I'm leaving out the rest to keep it short
cb_b = Checkbutton(master, ...)
cb_c = Checkbutton(master, ...)
cb_d = Checkbutton(master, ...)
cb_e = Checkbutton(master, ...)
# create an iterable of all the checkboxes
checkboxes = (cb_a, cb_b, cb_c, cb_d, cb_e)
def place_checkboxes(checkboxes):
for x, cb in enumerate(checkboxes):
cb.place(x=x * 100, y=20)
place_checkboxes(checkboxes) # call the function to place the cbs
def selectAllRating(checkboxes):
for cb in checkboxes:
cb.invoke() # 'invoke' selects and calls the command bound to each checkbox