带有嵌入小部件的 Perl Tk 列表框?

发布于 2024-12-05 11:40:00 字数 98 浏览 0 评论 0原文

有谁知道 Perl Tk 中是否有一种类型的列表框允许您在列表中包含一个小部件?例如,我需要一个多列列表框,其中其中一列包含用户可以选中或取消选中的 Checkbutton 小部件。

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 a Checkbutton widget that the user can check or uncheck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断桥再见 2024-12-12 11:40:01

据我所知,标准列表框小部件仅支持一行字符串作为内容。
不要沮丧,因为我们可以轻松构建一个更强大的列表框,其中可以包含小部件!

这是我为程序编写的一些代码。结果如下图(我画的,所以直接从相关文档中拿来的)
在此处输入图像描述

提示:
- 我编写的代码是使用 Tcl::Tk 模块在 Perl 中编写的,因此您可以轻松地将其更改为 Perl/Tk
- 如图所示,6个小部件组成一个数组@item,6个@item数组组成一个数组@cur_items,您可以从@cur_items开始查找此列表中的任何小部件(因为您已经知道顺序)
- 我使用额外的框架来使这些小部件整齐,如果您只想组织两个小部件,可以忽略它们。(但我建议使用框架)
- 请忽略小部件的名称,例如 subsubwidget 或 subsubdate,编码人员在繁重的编程压力下很难给出好听的名称:)

# 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

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)
enter image description here

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文