Reputation: 951
I am creating a Tk toplevel window with a lot of widgets that wont fit into a single screen, so I decided to add a vertical scrollbar to my window. The base of my GUI was toplevel window $w and frame $w.baseframe.
When I found out that frames in Tk cannot be scrollable and I also can't use the BWidgets scrolledframe, I decided to go with a scrollable Text widget $w.text instead of my $w.baseframe frame. I put all my children widgets into it and added a vertical scrollbar like this:
text $w.frame -yscrollcommand {$w.scrollbar set}
scrollbar $w.scrollbar -command {$w.text yview}
<here I set all my other widgets>
<here I pack all my other widgets into the $w.text>
pack $w.scrollbar -side right -fill y
pack $w.frame -side left -fill both -expand 1
When I run the script, all the widgets are displayed correctly in the Text widget, I can see the vertical scrollbar on the right side of the window, but the scrollbar doesent do anything, It doesent scroll up or down and half of my widgets are invisible, because the toplevel window is simply too tall.
Thank you for your advice!
EDIT
This is a very simplified version of my code. I add 200 buttons in a single column. When I run thius code, I see a toplevel window streatched from the top of my screen to the bottom, I see a scrollbar on the right but it isn't working.
set w .testwindow
catch {destroy $w}
toplevel $w
wm title $w "title"
text $w.text -yscrollcommand {$w.scrollbar set}
scrollbar $w.scrollbar -command {$w.text yview}
for {set i 0} {$i < 200} {incr i} {
frame $w.text.$i
}
for {set i 0} {$i < 200} {incr i} {
button $w.text.$i.btn -text "btn $i"
pack $w.text.$i.btn
}
for {set i 0} {$i < 200} {incr i} {
pack $w.text.$i
}
pack $w.scrollbar -side right -fill y
pack $w.text -side left -fill both -expand 1
Upvotes: 1
Views: 1283
Reputation: 71598
It should be running fine, but I think that you forgot to change the names of a few variables, for example, your text widget is called $w.frame
so your scrollbar should be set to this instead of $w.text
:
scrollbar $w.scrollbar -command {$w.frame yview}
When I run the above with this amended line, it works fine for me.
EDIT:
Here is a similar text widget with the buttons:
set w .testwindow
catch {destroy $w}
toplevel $w
wm title $w "title"
text $w.text -yscrollcommand {$w.scrollbar set}
scrollbar $w.scrollbar -command {$w.text yview}
for {set i 0} {$i < 200} {incr i} {
$w.text window create end \
-create "button %W.click$i -text \"btn $i\" \
-command {do stuff}"
$w.text insert end "\n"
}
pack $w.scrollbar -side right -fill y
pack $w.text -side left -fill both -expand 1
Note that I am using $w.text window create
instead which is specific to the text widget and which allows for embedding various other widgets in the text widget.
Also, if you are not using an 'external' variable like $i
in the example, braces would avoid those backslashes:
$w.text window create end -create {
button %W.click -text "btn" -command "do stuff"
}
Upvotes: 2