0

Been rapid firing questions recently. Hopefully this is the last one pertaining to scrollbars. I have a Text widget that populates over a loop. I want a scrollbar for all of the text fields. With the following implementation, the scrollbar only scrolls the last text field.

fldrs = ['C:\\Program Files\\Telegram\\Downloads',
    'C:\\ProgramData\\Microsoft\\Windows Defender Advanced Threat Protection\\Downloads',
    'C:\\Users\\All Users\\Microsoft\\Windows Defender Advanced Threat Protection\\Downloads',
    'C:\\Users\\Default\\Downloads', 'C:\\Users\\MaLonn\\Downloads',
    'C:\\Users\\Public\\Downloads']

root = T.Tk()
frm1 = ttk.Frame(root, relief='ridge', borderwidth=2)
frm1.grid()
frm2 = ttk.Frame(root, relief='ridge', borderwidth=2)
frm2.grid(row=1)
lbl1 = ttk.Label(frm1, text='To restore your open folders after'
    ' Explorer closes, select the windows\nyou have opened from the '
    'list below:')
lbl1.grid()
scrb = ttk.Scrollbar(frm2, orient='horizontal')
chkb_dict = {}
i = 2
for f in fldrs:
    chkb_dict[f] = T.IntVar()
    chkb = ttk.Checkbutton(frm2, variable=chkb_dict[f])
    chkb.grid(row=i, column=0)
    txt1 = T.Text(frm2, height=1, width=10, wrap='none',
            xscrollcommand=scrb.set)
    txt1.grid(row=i, column=1)
    txt1.insert(T.END, f + '\n')
    i += 1
scrb.config(command=txt1.xview)
scrb.grid(row=i + 1, column=1, columnspan=2, sticky='ew')

How do I get the scrollbar to scroll all of the text fields at once?

malonn
  • 59
  • 1
  • 7
  • You attach the scrollbar only to the last `Text` widget that you have created using the `scrb.config(command=txt1.xview)`. Try using something like [this](https://stackoverflow.com/a/66215091/11106801) but you will have to change the scrollbar from a vertical one to a horizontal one. – TheLizzard May 31 '21 at 16:47
  • EDIT Misread your post. I'll check out that link you posted... – malonn May 31 '21 at 17:45
  • Are you wanting to scroll everything as a single group, or are you wanting to scroll each widget individually (eg: does the text widget scroll up, or does each text widget scroll up one line)? – Bryan Oakley May 31 '21 at 17:48
  • @malonn Each time the for loop runs, the variable `txt1` is set to the last `Text` widget. So **after** the for loop when you call `scrb.config(command=txt1.xview)`, it uses only the last `Text` widget. Btw moving that line inside the for loop isn't going to work either because the scrollbar can only have 1 command. – TheLizzard May 31 '21 at 17:49
  • @BryanOakley My guess is that OP wants to scroll all of them at the same time. Each line consists of a `Checkbutton` and a `Text` widget. Also the scrollbar is horizontal. – TheLizzard May 31 '21 at 17:50
  • Yes, I want to scroll all of them at the same time. – malonn May 31 '21 at 18:12
  • I still don't understand. "All of them at the same time" could mean two different things. If you move the scrollbar, do you want the _entire widget_ to move up, or do the widgets stay in the same place and only the _contents_ of the widget move up? – Bryan Oakley May 31 '21 at 18:33
  • Well, it's horizontal scrolling. But I just want the contents of the `Text()` to scroll horizontally. Right now, the code works, except the horizontal scrollbar only scrolls the last text contents. I want it to scroll all of the text widgets (contents) at the same time. – malonn May 31 '21 at 18:48
  • I figured out a way to do it by inserting the checkbox into the text widget. Thanks, @BryanOakley and TheLizzard. – malonn May 31 '21 at 19:06

0 Answers0