Using Perl Tkx, I'm attempting to create a window with a treeview widget and a scrollbar to the right of it. I need the treeview widget to automatically resize when the user resizes the window.
This is what I have:
my $mw = Tkx::widget->new(".");
my $frm = $mw->new_ttk__frame(-padding => "2 6 12 12");
$frm->g_grid(-column => 0, -row => 0, -sticky => "nwes");
$frm->g_pack(-expand => 1, -fill => 'both');
my $tree = $frm->new_ttk__treeview;
$tree->g_grid(-column => 1, -columnspan => 5, -row => 1, -sticky => "we");
$tree->g_pack(-expand => 1, -fill => 'both');
my $scrollbar = $frm->new_ttk__scrollbar(-orient => 'vertical', -command => [$tree, 'yview']);
$scrollbar->g_grid(-column => 6, -row => 1, -sticky => "we");
$scrollbar->g_pack(-expand => 1, -fill => 'both');
$tree->configure(-yscrollcommand => [$scrollbar, 'set']);
Both widgets are displayed in the window, and the resizing works, but unfortunately the scrollbar is placed underneath the tree, not to the right of it. If I remove the three g_pack(-expand => 1, -fill => 'both')
lines, the positioning is correct, but the resizing doesn't work. How can I place the scrollbar to the right of the tree, and have the automatic resizing work?
You're using both grid
and pack
to layout widgets into the same container, which is not supported. The first step would be to use only pack
everywhere since you have a simple arrangement for the widgets.
Using -expand => 1
for the scrollbar means that Tk will try and give it as much space as possible. You don't want that for the scrollbar since it should only be allocated just enough space for itself. I have changed -fill
to 'y'
as a matter of style, but it doesn't seem to make much of a difference.
$scrollbar->g_pack(-expand => 0, -fill => 'y');
pack
will arrange widgets one below the other by default. So, you should add a -side
parameter if you need a horizontal arrangement starting from the left:
$tree->g_pack(-expand => 1, -fill => 'both', -side => 'left');
$scrollbar->g_pack(-expand => 0, -fill => 'y', -side => 'left');
I recommend you read through the excellent Mastering Perl/Tk book, especially the section about the pack
geometry manager. This book uses the Tk module
and not Tkx, but I think it should be easy to map concepts between both.