I tried to create a multiple checkboxes and get the information whether they are checked or not. I tried to use tkinter for this purpose. The number of checkboxes would be variable. Up to now, i found a way to create the checkboxes with the following code. With this, 10 checkboxes are created at which people can tick any of them
class Example(tk.Frame):
def __init__(self, root, *args, **kwargs):
tk.Frame.__init__(self, root, *args, **kwargs)
self.root = root
self.vsb = tk.Scrollbar(self, orient="vertical")
self.text = tk.Text(self, width=40, height=20,
yscrollcommand=self.vsb.set)
self.vsb.config(command=self.text.yview)
self.vsb.pack(side="right", fill="y")
self.text.pack(side="left", fill="both", expand=True)
n=10
for i in range(n):
cb = tk.Checkbutton(self, text="Modul %s" % i)
self.text.window_create("end", window=cb)
self.text.insert("end", "\n")
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
However, the information are not saved in any variable. If I add variable to be dumped in the cb, the code will check every checkboxes. The edited code section is as follow (sorry that I couldn't highlight the addition):
class Example(tk.Frame):
def __init__(self, root, *args, **kwargs):
tk.Frame.__init__(self, root, *args, **kwargs)
self.root = root
self.vsb = tk.Scrollbar(self, orient="vertical")
self.text = tk.Text(self, width=40, height=20,
yscrollcommand=self.vsb.set)
self.vsb.config(command=self.text.yview)
self.vsb.pack(side="right", fill="y")
self.text.pack(side="left", fill="both", expand=True)
n=10
var1 = IntVar()
val =[]
for i in range(n):
cb = tk.Checkbutton(self, text="Modul %s" % i, variable=var1)
self.text.window_create("end", window=cb)
self.text.insert("end", "\n") # to force one checkbox per line
val.append(var1.get())
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
Could you help what can I add to the code in order to be able to get the checked Modules by the user? for insntance i will get in "val" variable a list with [1, 0, 1, 0, 0, 0, 1, 0, 0, 0] if somebody tick module 0, 2, and 6
I look forward for your feedback.
thank you in advance, fadri
Aucun commentaire:
Enregistrer un commentaire