如何获取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"].