如何获取 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...
我知道我可以这样做:
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?
要从标签中获取值,您可以使用 cget
方法,可用于获取任何配置选项的值.
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"]
.