如何在 Perl Tk 中更改标签中的数据?

编程入门 行业动态 更新时间:2024-10-25 02:20:49
本文介绍了如何在 Perl Tk 中更改标签中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-30 08:58:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391958.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标签   数据   如何在   Tk   Perl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!