python - Adding a list of checkbuttons to a scrolled text widget -
i need have checkbuttons inside scrolled text widget, 1 checkbutton per row , text behind each button. found solution stackoverflow:
tkinter checkbuttons inside of text widget , scrolling
here's code snipped:
for in range(1000): bg = 'grey' if % 2 == 0: bg = 'white' cb = tk.checkbutton(text="checkbutton #%s" % i, bg=bg, width=35, justify=tk.left) text.window_create("end", window=cb) text.insert("end", "\n") # force 1 checkbox per line
this doesn't make sense me because while checkbuttons displayed correctly don't have access each of them. or wrong?
like other python object, in order call methods on need have reference. simplest solution keep references widget , variable in list or dictionary.
for example:
import tkinter tk class example(object): def __init__(self): root = tk.tk() text = tk.text(root, cursor="arrow") vsb = tk.scrollbar(root, command=text.yview) button = tk.button(root, text="get values", command=self.get_values) text.configure(yscrollcommand=vsb.set) button.pack(side="top") vsb.pack(side="right", fill="y") text.pack(side="left", fill="both", expand=true) self.checkbuttons = [] self.vars = [] in range(20): var = tk.intvar(value=0) cb = tk.checkbutton(text, text="checkbutton #%s" % i, variable=var, onvalue=1, offvalue=0) text.window_create("end", window=cb) text.insert("end", "\n") self.checkbuttons.append(cb) self.vars.append(var) text.configure(state="disabled") root.mainloop() def get_values(self): cb, var in zip(self.checkbuttons, self.vars): text = cb.cget("text") value = var.get() print("%s: %d" % (text, value)) if __name__ == "__main__": example()
Comments
Post a Comment