问题描述
限时送ChatGPT账号..我正在尝试使用 Tk 创建一个程序,该程序将从条目中获取数据,然后单击按钮创建一个包含该数据的标签.
I'm trying to create a program with Tk that will take the data from an entry, and, at the click of a button, create a label that has that data.
下面是我一直在调试的代码.在调试过程中,我试过tb]geh如下:
Below is the code I've been debugging. In the process of debugging, I have tried tb]geh following:
使用对$printItem
的引用将子程序连接到 -command
转到子程序以各种方式结合上述内容
use Tk; use strict; use warnings;
$mw = MainWindow -> new;
my $printItem = $mw -> Entry(-width = 20); $printItem -> pack;
$mw -> Button(-text => "Write.", -command => sub{ $mw -> Label(-text => "$printItem") -> pack} -> pack;
MainLoop;
当我点击按钮时,标签显示的是Tk::Entry=HASH([这里似乎是随机的十六进制数])
.这显然不是我想要的,我想知道如何才能达到我想要的效果.
When I click the button, all that the label shows is Tk::Entry=HASH([seemingly random hexadecimal number here])
. This is obviously not what I want, and I'd like to know how I can get the effect I desire.
推荐答案
Tk::Entry=HASH(0xdeadbeef)
是 Perl 字符串化对象的方式.事实上,你的 $printItem
变量存储了一个 Tk::Entry
类的对象:
Tk::Entry=HASH(0xdeadbeef)
is how Perl stringifies objects. And indeed, your $printItem
variable stores an object of class Tk::Entry
:
my $printItem = $mw -> Entry(-width = 20);
从 Tk::Entry 小部件获取字符串,你可以使用它的 get
方法:
To get the string from a Tk::Entry widget, you can use its get
method:
... -command => sub { $mw->Label(-text => $printItem->get)->pack } ...
完整的工作示例:
use strict;
use warnings;
use Tk;
my $mw = MainWindow->new;
my $printItem = $mw->Entry(-width => 20); $printItem->pack;
$mw->Button(-text => "Write.", -command => sub { $mw->Label(-text => $printItem->get)->pack })->pack;
MainLoop;
这篇关于如何在 Perl Tk 中更改标签中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论