Dirty Penguin
Dirty Penguin

Reputation: 4402

Tkinter: Working with Checkbutton + Scrollbar inside a Text widget

I've created two Text box widgets side-by-side. The left Text box contains 100 rows of Checkbutton boxes. The right Text box contains 100 rows of text. I've configured both Text boxes to have the same height of 30 and width of 10.

When I run the program, the left Text box grows vertically beyond the size of my view able screen area. As a result, the right Text box is pushed so far down the Frame that I can no longer see it. I know it's there, because when I reduce the number of rows (for both boxes) to 50 (as opposed to 100), I can see the right Text box.

Also, I noticed that the left Scrollbar isn't connected to the left Text box. However, the right Scrollbar is correctly connected to the right Text box.

Could you please help me understand:

1) Why does the left Text box grow vertically (outside the defined Text widget size) and out of sync with the right Text box size and position?

2) Why doesn't the left Scrollbar attach to the left Text widget (like the right Text box)?

Here is my code so far:

#!/usr/bin/python
from Tkinter import *

class MyApp:
    def __init__(self, parent):
       self.myParent = parent
       self.main_container = Frame(self.myParent)
       self.main_container.grid(row=0, column=0, columnspan=4)

       self.left_frame = Frame(self.main_container)
       self.left_frame.grid(row=0, column=0, columnspan=2)

       self.right_frame = Frame(self.main_container)
       self.right_frame.grid(row=0, column=2, columnspan=2)

       self.checkbox_scrollbar = Scrollbar(self.left_frame)
       self.checkbox_scrollbar.grid(row=0, column=1, sticky='NS')
       self.checkbox_text = Text(self.left_frame, height=30, width=10, yscrollcommand=self.checkbox_scrollbar.set)
       self.checkbox_text.grid(row=0, column=0)
       self.checkbox_scrollbar.config(command=self.checkbox_text.yview)

       self.databox_scrollbar = Scrollbar(self.right_frame)
       self.databox_scrollbar.grid(row=0, column=1, sticky='NS')
       self.databox_text = Text(self.right_frame, height=30, width=10, yscrollcommand=self.databox_scrollbar.set)
       self.databox_text.grid(row=0, column=0)
       self.databox_scrollbar.config(command=self.databox_text.yview)

    my_dict = {}
    for each_num in range(100):
        my_line = "Line number: " + str(each_num)
        my_dict[my_line] = 0

    for my_key in my_dict:
        my_dict[my_key] = IntVar()
        cb = Checkbutton(self.checkbox_text, text=my_key, variable=my_dict[my_key])
        cb.grid(sticky='W')

    for each_entry in range(100):
        entry_line = "This is entry number: " + str(each_entry) + "\n"
        self.databox_text.insert(END, entry_line)


root = Tk()
root.title("Checkbox UI Test")
myapp = MyApp(root)
root.mainloop()

Upvotes: 2

Views: 3309

Answers (2)

Dirty Penguin
Dirty Penguin

Reputation: 4402

I needed to use the Text widget's method called window_create to insert my embedded object (my checkbutton widget). Here is how to implement it. Hope this helps others:

for my_key in my_dict:
    my_dict[my_key] = IntVar()
    cb = Checkbutton(text=my_key, variable=my_dict[my_key])
    self.checkbox_text.window_create("END", window=cb)
    self.checkbox_text.insert("END", "\n")

Upvotes: 1

TankorSmash
TankorSmash

Reputation: 12747

I've been at this an hour, but my Tkinter knowledge isn't as complete as I'd like, so I'm sure someone else will come by with another answer but:

The Text box isn't actually being shown, as far as I can tell, it's being hidden by the CheckBoxs that come after. Try checkbox_text.insert(END, "some text") and change the background to "red" and you'll see that it's not visible on screen, even if you change the 100 checkboxes to only draw 2. I'm not sure why the height limit of the Text isn't being respected, but the grid'ing of the CheckBoxs must have something to do with it.

The same issue is affecting the ScrollBar, it's attached to the left Frame, not the Text; I haven't tried it, but maybe creating it after you create the Text you want to attach it to could be helpful.

Your other question about it being out of sync could be a matter of setting weight, like this, but it didn't work for me. A reason I think that could be is that they're not part of the same grid, as in they can't communicate their size to each other. Both of the Frames are the same size, which is the best it can do, I think.

It's late and I'm probably wrong about stuff here, so please take this all with a grain of salt.

Some debugging stuff I like to do if .config(bg="RED") on widgets related to my issue and sometimes that'll help me visualize exactly what's going on.

Upvotes: 1

Related Questions