0

I am trying to add a scroll bar to a text box which makes up part of my GUI.

So far I have made the text box and (i think) the scroll bar but dont know how to combine the two items.

textBox_1 = Text(myGUI).place(x=75, y=300)
scroll_1 = Scrollbar(myGUI)
scroll_1.configure()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

3 Answers3

1

There are two steps that you need to take: you need to connect the scrollbar to the widget, and you need to connect the widget to the scrollbar. For example:

textBox_1 = Text(...)
scroll_1 = Scrollbar(...)
textBox_1.configure(yscrollcommand=scroll_1.set)
scroll_1.configure(command=textBox_1.yview)

Also, I notice that you called place as part of widget creation. You cannot do that. When you do Text(...).place(...) it stores the result of place in textbox_1, not the result of Text(...). Plus, it's just easier to maintain your code when the layout is separate from widget creation.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

Should be:

textbox_1.config(yscrollcommand=scroll_1.set)
stark
  • 12,615
  • 3
  • 33
  • 50
  • i get an AttributeError: NoneType' object has no attribute 'config' –  Feb 17 '13 at 19:21
  • Putting place in the command means it is not returning the Text widget. Separate onto two lines. – stark Feb 17 '13 at 19:30
0

When using Tkinter, no matter what geometry manager you're using, you need to create your widget and use the geometry manager on separate lines if you want to keep a reference to the Widget. In other words, Widget.place returns None (as does Widget.pack and Widget.grid).

textBox_1 = Text(myGUI)
textBox_1.place(x=75, y=300)
scroll_1 = Scrollbar(myGUI)
textbox_1.config(yscrollcommand=scroll_1.set)
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • it compiles but the scroll bar isn't on the textbox –  Feb 17 '13 at 19:30
  • You still need to pack or place the scrollbar – stark Feb 17 '13 at 19:32
  • is there a way i can make it stretch so its the same height as the text box? –  Feb 17 '13 at 19:36
  • I've never used a scrollbar with place. Can you use pack? – stark Feb 17 '13 at 19:42
  • @stark: you can use pack, place or grid. I can't imagine a scenario where place makes the most sense. With all three there are configuration options to have the widget stretch or shrink, though the options are different for each (since they work in fundamentally different ways). – Bryan Oakley Feb 18 '13 at 03:14