PerlTk标签-同一小部件中的不同颜色文本

PerlTk标签-同一小部件中的不同颜色文本,perl,tk,perltk,Perl,Tk,Perltk,我已经编写了一个与我们的psql数据库交互的GUI。对于给定的日期,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

我已经编写了一个与我们的psql数据库交互的GUI。对于给定的日期,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”涂成红色吗

通过在带有字符串参数的
Tk::Table
上使用
->put
方法,可以创建一个简单的
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个字符,并且不会根据其内容自动收缩或扩展。在这个示例中,我使用了一个hack,使用
GeometryRequest
强制使用与等效标签小部件相同的几何体。也许你可以改为硬编码
-width
-height
。另一种解决方案是使用
Tk::Text
/
Tk::ROText
bbox()
方法来计算几何体