Reputation: 3111
Using the code found in this stackoverflow post, I have altered it slightly to include a background color for every other checkbutton and set the width to fill up the width of the Text widget. However, when this happens, I can not use my mouse wheel to scroll. I have to grab onto the scroll bar.
Is there a better way to do this that would allow normal scrolling? Here is my code:
import Tkinter as tk
root = tk.Tk()
vsb = tk.Scrollbar(orient="vertical")
text = tk.Text(root, width=40, height=20, yscrollcommand=vsb.set)
vsb.config(command=text.yview)
vsb.pack(side="right",fill="y")
text.pack(side="top",fill="both",expand=True)
for i in range(1000):
bg = 'grey'
if i % 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") # to force one checkbox per line
root.mainloop()
Upvotes: 4
Views: 2262
Reputation: 2414
The problem is that when you fill up the Text widget with Checkbuttons, you now interact with the Checkbuttons' event bindings when your mouse is over the window (and not the Text widget which is "hidden"). Whereas the Text widget has bindings for mouse scrolling (button 4 and 5), the Checkbuttons don't automatically come with it. And (AFAIK) parent widgets can not be automatically set to receive any events through their children, so you have to do it manually. The following code will work (insert after cb
is created):
cb.bind('<Button-4>', lambda event: text.yview_scroll(-1, tk.UNITS))
cb.bind('<Button-5>', lambda event: text.yview_scroll( 1, tk.UNITS))
Upvotes: 2