开发者

Perl Tk listbox with embeded widgets?

does anyone know if there is a type of listbox in Perl Tk that allows you to have a widget in the list? For example, I need a multi-column listbox with one of the columns containing 开发者_Python百科a Checkbutton widget that the user can check or uncheck.


As far as I know, standard listbox widget only supports one line of string as content. Don't be frustrated, because we can build a more powerful listbox, which CAN contain widgets, easily!

Here are some codes I wrote for a program. And the result is like the pic shown below(I draw it, so I take it directly from the related document)

Perl Tk listbox with embeded widgets?

The TIPS:
- The code I wrote is in Perl using Tcl::Tk module, so you can easily change it into Perl/Tk
- As the pic shows, 6 widgets build up an array @item, and 6 @item arrays build up an array @cur_items, and you can start from @cur_items to find any widget in this list (as you have already known the sequence)
- I use additional frames to make these widgets in tidy, you can ignore them if you only want to organize two widgets.(But I suggest using frame)
- Please ignore the widget's name, like subsubwidget or subsubdate, it is hard for coders to give nice names under heavy programming pressure :)

# init - create six items
# each of them contains 5 widgets
for (1..6) {
    my @items = ();

    # create a frame for each item
    # 0 --- LabelFrame --- file name
    # 1 --- label --- icon
    # 2 --- text --- path dir
    # 3 --- label --- date
    # 4 --- label --- size
    # 5 --- button --- preview info
    my $item = $toplevel->Frame(
        -bg => 'white',
    )->pack(
        -in => $toplevel,
        -anchor => 'nw',
        -pady => 10,
    );
    my $widget = $item->LabelFrame(
        -bg => 'white',
        -fg => 'blue',
    )->pack(
        -in => $item,
        -side => 'left',
        -anchor => 'nw',
    );
    push (@items, $widget);
    # file/dire type icon
    my $subicon = $widget->Label(
        -bg => 'white',
    );
    $subicon->pack(
        -in => $widget,
        -side => 'left',
        -anchor => 'center'
    );
    push (@items, $subicon);
    my $subwidget = $widget->Frame(
        -bg => 'white',
    )->pack(
        -in => $widget,
        -side => 'left',
    );
    # file path
    my $subtext = $subwidget->Text(
        -height => 2,
        -width => 39,
        -bg => 'white',
        -borderwidth => 0,
        -wrap => 'char',
    )->pack(
        -in => $subwidget,
        -anchor => 'nw',
    );
    push (@items, $subtext);
    $subtext->configure(-state => 'disabled');
    # date and size
    my $subsubwidget = $subwidget->Frame(
        -bg => 'white',
    )->pack(
        -in => $subwidget,
        -anchor => 'nw',
    );
    my $subsubdate = $subsubwidget->Label(
        -bg => 'white',
        -text => '',
    )->pack(
        -in => $subsubwidget,
        -anchor => 'nw',
        -side => 'left',
    );
    push (@items, $subsubdate);
    my $subsubsize = $subsubwidget->Label(
        -width => 12,
        -bg => 'white',
    )->pack(
        -in => $subsubwidget,
        -anchor => 'nw',
        -side => 'left',
    );
    push (@items, $subsubsize);
    # more info
    my $infobtn = $item->Button(
        -text => '>',
        -width => 0,
        -height => 4,
        -padx => 0,
        -pady => 0,
        -relief => 'flat',
        -bg => 'white',
    )->pack(
        -in => $item,
        -side => 'left',
        -anchor => 's',
    );
    push (@items, $infobtn);

    # record created widget
    push (@cur_items, \@items);
}
# end of init


Tk::HList might be what you're looking for. See:

http://www.perltk.org/index.php?option=com_content&task=view&id=23&Itemid=28

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜