Perl Tk 设置按钮中文本的格式

发布于 2025-01-11 21:56:09 字数 1289 浏览 0 评论 0原文

下面的代码创建 Perl/Tk 按钮,其中填充了哈希 %events 中每个事件的详细信息。有没有一种方法可以格式化按钮内的文本,以便它以 printf 在第 16 行打印出来的方式显示在对齐的列中。

use strict;
use Tk;

my %events;
$events{"Jazz Fest"}{city}   = "New Orleans";
$events{"Jazz Fest"}{state}   = "LA";
$events{"Jazz Fest"}{date}   = "June 6th, 2023";
$events{"Lollapalooza"}{city}   = "Chicago";
$events{"Lollapalooza"}{state}   = "IL";
$events{"Lollapalooza"}{date}   = "July 25th, 2023";
$events{"Blues Fest"}{city}   = "Chicago";
$events{"Blues Fest"}{state}   = "IL";
$events{"Blues Fest"}{date}   = "Augsut 4th, 2023";

my %data;
foreach my $event (sort keys %events) {

    printf("%-15.15s %-20.20s %-2.2s %18.18s\n", $event, $events{$event}{city}, $events{$event}{state}, $events{$event}{date});
    $data{$event} = sprintf("%-15.15s %-20.20s %-2.2s %18.18s", $event, $events{$event}{city}, $events{$event}{state}, $events{$event}{date});
    
}

my $data;

my $mw = MainWindow->new;
$mw->title("Event Clipboard Copy");
$mw->label("Pick Event Details to Copy.");

for(sort keys %data){

    my $text = $data{$_};
    $mw->Button( -text => $text, -anchor => 'w', -command => sub  {
    
    Clipboard->copy( $text );
         
        }
    )->pack( -fill => 'x' );
}

MainLoop;

The code below creates Perl/Tk buttons filled with each event's details from the hash %events. Is there a way to format the text inside the buttons so that it appears in aligned columns in the manner printf prints it out on line 16.

use strict;
use Tk;

my %events;
$events{"Jazz Fest"}{city}   = "New Orleans";
$events{"Jazz Fest"}{state}   = "LA";
$events{"Jazz Fest"}{date}   = "June 6th, 2023";
$events{"Lollapalooza"}{city}   = "Chicago";
$events{"Lollapalooza"}{state}   = "IL";
$events{"Lollapalooza"}{date}   = "July 25th, 2023";
$events{"Blues Fest"}{city}   = "Chicago";
$events{"Blues Fest"}{state}   = "IL";
$events{"Blues Fest"}{date}   = "Augsut 4th, 2023";

my %data;
foreach my $event (sort keys %events) {

    printf("%-15.15s %-20.20s %-2.2s %18.18s\n", $event, $events{$event}{city}, $events{$event}{state}, $events{$event}{date});
    $data{$event} = sprintf("%-15.15s %-20.20s %-2.2s %18.18s", $event, $events{$event}{city}, $events{$event}{state}, $events{$event}{date});
    
}

my $data;

my $mw = MainWindow->new;
$mw->title("Event Clipboard Copy");
$mw->label("Pick Event Details to Copy.");

for(sort keys %data){

    my $text = $data{$_};
    $mw->Button( -text => $text, -anchor => 'w', -command => sub  {
    
    Clipboard->copy( $text );
         
        }
    )->pack( -fill => 'x' );
}

MainLoop;

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

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

发布评论

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

评论(1

哎呦我呸! 2025-01-18 21:56:09

要对齐列,请将每个按钮的字体设置为“Courier”(或其他等宽字体)。至少有两种方法可以做到这一点:

  1. 创建按钮时
  2. 将按钮分配给变量,然后对其调用 configure
#Setting font when creating the button
$mw->Button( -text => $text, -anchor => 'w', -font => [-family => 'Courier'], -command => sub {
#Calling configure on the button
my $button = $mw->Button(-text => $text, -anchor => 'w', -command => sub {
...
}->pack( -fill => 'x' );
$button->configure(-font => [-family => 'Courier']);

这是字体设置为 Courier 时的外观
对话框字体设置为 Courier

并且不设置字体:

未对齐的列

To align the columns, set font of each button to "Courier" (or another monospaced font). There are at least two ways to do this:

  1. when creating the button
  2. assign the button to a variable and then call configure on it.
#Setting font when creating the button
$mw->Button( -text => $text, -anchor => 'w', -font => [-family => 'Courier'], -command => sub {
#Calling configure on the button
my $button = $mw->Button(-text => $text, -anchor => 'w', -command => sub {
...
}->pack( -fill => 'x' );
$button->configure(-font => [-family => 'Courier']);

Here's how it looks with font set to Courier
dialog with font set to Courier

And without setting font:

unaligned columns

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