如何获取Tkinter标签文本? [英] How to get the Tkinter Label text?

查看:1502
本文介绍了如何获取Tkinter标签文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将列出用户将从中选择的地址,然后将返回地址文本.我需要使用Tkinter.Label,因为Tkinter.Listbox不允许换行.

Im making a list of addresses that the user will select from, and the address text will be returned. I need to use Tkinter.Label because the Tkinter.Listbox will not allow for newlines.

踢球者在Label类中没有类似于.get()的方法...

The kicker is there is no .get()-like method in the Label class...

我知道我可以做类似的事情:

I know I can do something like:

v = StringVar()
Label(master, textvariable=v).pack()
v.set("New Text!")
 ...
print v.get()

但是,我有一个5-20个地址的列表,其中包含一个单独的StringVar()数组,这将很困难b/c我无法识别活动标签的位置.我只想访问激活的窗口小部件内容.

However, I have a list of 5-20 address' keeping a seperate array of StringVar()'s will be difficult b/c I have no way of identifying the loc of the active label. I would like to just access the activated widget contents.

Tkinter.Label使用的是正确的小部件吗?

Is Tkinter.Label the right widget to be using?

推荐答案

要获取标签中的值,可以使用

To get the value out of a label you can use the cget method, which can be used to get the value of any of the configuration options.

例如:

l = tk.Label(text="hello, world")
...
print("the label is", l.cget("text"))

您还可以使用选项作为键,将该对象视为字典.使用相同的示例,您可以使用l["text"].

You can also treat the object as a dictionary, using the options as keys. Using the same example you can use l["text"].

这篇关于如何获取Tkinter标签文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆