有没有人知道Perl Tk中是否有一种列表框可以让你在列表中有一个小部件?例如,我需要一个多列列表框,其中一列包含用户可以选中或取消选中的Checkbutton小部件。
答案 0 :(得分:1)
据我所知,标准列表框小部件仅支持一行字符串作为内容。 不要感到沮丧,因为我们可以轻松构建一个更强大的列表框,它可以包含小部件!
以下是我为程序编写的一些代码。结果如下图所示(我绘制它,所以我直接从相关文档中获取)
提示:
- 我写的代码是在Perl中使用Tcl :: Tk模块,因此您可以轻松将其更改为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
答案 1 :(得分:0)
Tk :: HList可能就是你要找的东西。见:
http://www.perltk.org/index.php?option=com_content&task=view&id=23&Itemid=28