Tkinter 中的标签:更改文本

问题描述 投票:0回答:3

我正在尝试更改 tkinter 中标签文本的值,我正在使用

label.config()
但似乎以前的值仍然以新值出现在屏幕上。

这是我正在使用的代码:

This is the code that I'm using

这是结果,之前的和新的文本在一起:

This is the result, the previous and the new text are together

python tkinter label
3个回答
3
投票

每当执行

select_description()
时,都会创建新标签并将其放入同一单元格中。这就是文本重叠的原因。

您需要在函数外部创建标签:

description_label = Label(frame1)
description_label.grid(row=4, column=0, columnspan=4)

def select_description(event):
    choice = list_profiles.get(ANCHOR)
    if choice == 1:
        description_label.config(text=...)
    elif choice == 2:
        description_label.config(text=...)

0
投票

执行此操作的快捷方式。不使用

==1
==2
。 片段:

description_label = Label(frame1)
description_label.grid(row=4, column=0, columnspan=4)

def select_description(event):
     choice = list_profiles.get(ANCHOR)
    if choice:
        description_label.config(text=...)
    else:
        description_label.config(text=...)

0
投票

这在我的环境中不起作用。

错误:

    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
    _tkinter.TclError: unknown option "-text"

还有其他解决办法吗?什么版本/解释器?

© www.soinside.com 2019 - 2024. All rights reserved.