我已经编写了一个GUI,它与我们的psql数据库交互。对于给定的日期,gui显示一个包含各种标识符和信息位的人员列表。我使用Tk::Table来显示数据
eg
my $f_mainframe = $mw -> Frame(-bg=>'white');
$f_mainframe -> pack(-side=>'top', -expand=>1, -fill=>'both');
my $itable = $f_mainframe -> Table(-rows => 13,
-columns=>30,
-fixedrows => 1,
-fixedcolumns => 1,
-relief => 'raised') -> pack();
$itable->put(1,$firstnamecol,"First Name\nMYO");有没有可能把“名字”涂成黑色,把"MYO“涂成红色?
发布于 2013-11-22 14:47:26
通过在带有字符串参数的->put上使用Tk::Table方法,可以创建一个简单的Tk::Label小部件。标签只能配置为具有单一的前景色。要实现您想要的结果,您可以使用Tk::ROText (一个只读文本小部件)。下面的代码显示一个标签小部件和一个文本小部件,但后者具有不同的颜色:
use strict;
use Tk;
use Tk::ROText;
my $mw = tkinit;
# The monocolored Label variant
my $l = $mw->Label
(
-text => "First Name\nMYO",
-font => "{sans serif} 12",
)->pack;
# The multicolored ROText variant
my $txt = $mw->ROText
(
-borderwidth => 0, -highlightthickness => 0, # remove extra borders
-takefocus => 0, # make widget unfocusable
-font => "{sans serif} 12",
)->pack;
$txt->tagConfigure
(
'blue',
-foreground => "blue",
-justify => 'center', # to get same behavior as with Tk::Label
);
$txt->tagConfigure
(
'red',
-foreground => "red",
-justify => 'center', # to get same behavior as with Tk::Label
);
$txt->insert("end", "First Name\n", "blue", "MYO", "red");
# a hack to make the ROText geometry the same as the Label geometry
$txt->GeometryRequest($l->reqwidth, $l->reqheight);
MainLoop;正如您所看到的,要使文本小部件变体正常工作,需要更多的输入。因此,将此代码抽象为子例程或小部件类(可能对CPAN?)很有用。还请注意,您必须自己处理文本小部件的几何学。标签自动扩展以容纳标签内容。默认情况下,文本小部件的几何形状为80x24字符,并且不会根据其内容自动收缩或扩展。在示例中,我使用了一个使用GeometryRequest的hack来强制使用与等效label小部件相同的几何形状。也许你可以用硬编码的-width和-height代替。另一种解决方案是使用bbox()方法Tk::Text/Tk::ROText来计算几何形状。
https://stackoverflow.com/questions/20144919
复制相似问题