【问题标题】:Flashing a GUI button using Perl & Ttk on a Mac在 Mac 上使用 Perl 和 Ttk 闪烁 GUI 按钮
【发布时间】:2021-05-31 02:16:45
【问题描述】:

使用 Perl v5.28,Tkx.pm v1.10 ActiveState Tcl/TTk v8.6.9('aqua' 风格),在 macOS v10.13.6 上。这 下面的演示按需要工作,启用给定的调用 子程序使用 GUI 按钮按下鼠标,或使用 带有“普通”文本字符的键盘输入。

我希望拥有的一项附加功能是视觉反馈 当键盘被按下(闪烁)的图形按钮 使用替代激活。我发现了一个看起来像 Tcl 的东西 使用event generate 命令的解决方案,以及 使用 Perl 的参考资料 Tkx::event_generate() 虚拟事件方法调用。我什至找到了等效的 Perl Tkx::after(100) 函数调用来创建建议的延迟。但是我 无法理解如何将这一切放在一起以实现 想要的效果。任何帮助将不胜感激,理解 与其他一些 TTk 功能一样,这可能无法在 Mac 上运行。

代码

#!/usr/bin/env perl
# -*- cperl -*-

use warnings;
use strict;
use Tkx; $Tkx::TRACE = 1;
my $mw = Tkx::widget->new(".");
$mw->g_wm_geometry('200x200+100+100');

my $l = $mw->new_ttk__label
  (
    -text => ("Button Test Ttk v" . Tkx::info("patchlevel"))
  );

sub greeting { print(STDERR "--> [ Hello, world ] pressed\n") }

my $b = $mw->new_ttk__button
  (
         -text => "Hello, world",
    -underline => 0,
      -command => sub { greeting() }
  );

$mw->g_bind('<h>', sub { $b->invoke() });
$mw->g_bind('<H>', sub { $b->invoke() });
$l->g_pack();
$b->g_pack();
Tkx::MainLoop();

print(STDERR "Program exit...\n");

运行日志

Tkx-1-0.0s-demo-12: wm geometry . 200x200+100+100
Tkx-2-0.0s-demo-14: info patchlevel
Tkx-3-0.0s-demo-14: winfo children .
Tkx-4-0.0s-demo-14: ttk::label .l -text {Button Test Ttk v8.6.9}
Tkx-5-0.0s-demo-26: winfo children .
Tkx-6-0.0s-demo-26: ttk::button .b -text {Hello, world} -underline 0 -command perl::callback
Tkx-7-0.0s-demo-28: bind . <h> perl::callback
Tkx-8-0.0s-demo-29: bind . <H> perl::callback
Tkx-9-0.0s-demo-30: pack .l
Tkx-10-0.0s-demo-31: pack .b
--> [ Hello, world ] pressed
Tkx-11-6.4s-demo-28: .b invoke
--> [ Hello, world ] pressed
Tkx-12-7.9s-demo-29: .b invoke
--> [ Hello, world ] pressed
Program exit...

【问题讨论】:

  • 我无法让Tkx 在 macOS 11.2.3 上运行,请参阅this 问题。
  • 我感受到你的痛苦。我特别避免在我的个人项目中使用 macOS High Sierra (v10.13),以避免出现您所看到的问题。由于 Apple 正在删除所有脚本语言,我建议安装 ActiveState Tcl 包。 Perl Tkx 模块文档的底部是一个您可能需要定义的环境变量,以便 Tkx 可以找到 ActiveTcl 库。我喜欢 8.6 版本的 Ttk(主题 Tk),它使 Perl/Tk 应用程序在 Mac 上看起来是原生的。它还具有适用于各种版本的 Windows 和 Linux 的主题。

标签: perl tkx


【解决方案1】:

这是一个示例(在 Ubuntu 21.04 上测试)。通过在按钮上调用g_event_generate("&lt;ButtonPress-1&gt;"),按钮上会自动调用invoke()

use feature qw(say);
use strict;
use warnings;
use Tkx;
my $mw = Tkx::widget->new(".");
$mw->g_wm_geometry('200x200+100+100');

my $l = $mw->new_ttk__label(
    -text => ("Button Test Ttk v" . Tkx::info("patchlevel"))
);

sub greeting { say "--> [ Hello, world ] pressed"; }

my $b = $mw->new_ttk__button(
    -text      => "Hello, world",
    -underline => 0,
    -command   => sub { greeting() }
);
sub generate_button_click_event {
    $b->g_event_generate("<ButtonPress-1>");
    Tkx::after(
        200, sub {
            $b->g_event_generate("<ButtonRelease-1>");
        }
    );
}

$mw->g_bind('<h>', sub { generate_button_click_event() });
$mw->g_bind('<H>', sub { generate_button_click_event() });
$l->g_pack();
$b->g_pack();
Tkx::MainLoop();
say "Program exit...";

【讨论】:

    猜你喜欢
    • 2022-06-28
    • 1970-01-01
    • 2013-08-03
    • 2018-01-30
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多