python Tk 在我设置后保留标签文本

标签 python tkinter

这是一个极其简单的 Python Tk 程序。我似乎无法阻止一个简单的问题,而且我确信我错过了一些简单的事情。

我做了一个标签:

myLabelText = StringVar()
myLabelText.set("Something kinda long")
myLabel = Label(frame, textvariable=myLabelText).pack()

稍后在同一个程序中,我想更新该标签以表示“Foo”...

myLabelText.set("Foo")
frame.update_idletasks()

标签现在看起来像“Fooething kinda long” 目标是只包含“Foo”并清除其余的标签文本。

我尝试将标签设置为一长串空格,但由于某种原因,没有清除该字段中的文本。正确的做法是什么?


编辑

这是一个演示我的问题的完整示例。

from Tkinter import *
import tkFileDialog, Tkconstants
import time
import urllib2

def main():
    " Controlling function "

    root = Tk()
    app = App(root)
    root.mainloop()

class App:
    " Class for this application "

    def __init__(self, master):

        # Setup the window
        frame = Frame(master, width=400, height=200)
        frame.pack()
        frame.pack_propagate(0)
        self.frame = frame
        self.master = master

        # Start Button
        self.button = Button(frame, text='Start', bg="#339933", height=3, width=10, command=self.start)
        self.button.pack()

        # Label
        self.operation_action_text = StringVar()
        self.operation_action_text.set("Waiting on user to click start...")
        self.operation_action = Label(frame, textvariable=self.operation_action_text)
        self.operation_action.pack()


    def start(self):
        " Change the label "

        # Do something and tell the user
        response = urllib2.urlopen('http://www.kennypyatt.com')
        json_string = response.read()
        self.operation_action_text.set("Something kinda long")
        self.frame.update_idletasks()
        time.sleep(2)

        # Do something else and tell the user
        response = urllib2.urlopen('http://www.kennypyatt.com')
        json_string = response.read()
        self.operation_action_text.set("ABCDEFGHI")
        self.frame.update_idletasks()
        time.sleep(2)

        # Do a third thing and tell the user
        response = urllib2.urlopen('http://www.kennypyatt.com')
        json_string = response.read()
        self.operation_action_text.set("FOO")
        self.frame.update_idletasks()

        return

main()

最佳答案

您要考虑采取不同做法的第一件事就是分离:

myLabel = Label(frame, textvariable=myLabelText).pack()

进入

myLabel = Label(frame, textvariable=myLabelText)
myLabel.pack()

请阅读→this link←有关于此的信息。


要更新标签的文本,您不需要 control variable .
您可以通过以下两种方式之一更改文本:

myLabel.configure(text='new text')

myLabel['text'] = 'new text'

您可以检查第二个示例 →here←
进一步了解与此主题相关的控制变量。


update_idletasks()不需要更新标签小部件的文本。
我只发现在使用窗口的 geometry 时需要使用它.
其中一个例子是 centering a window .


基于编辑:

将 root 和 app 创建为全局函数的局部变量是非常不正统的。
尝试根据看到的结构重写 →here← .

您将希望避免在 tkinter 应用程序中使用 time.sleep() ;因为它与事件循环冲突。尝试将单个方法拆分为多个方法,然后您可以在每个方法末尾使用 Tk 的 after(milliseconds, method) 方法,在 x 毫秒后优雅地移至下一个方法。

我还会考虑从您的方法中删除 return

关于python Tk 在我设置后保留标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12022082/

相关文章:

java - 从java调用python文件会抛出错误

python - 如何通过单击按钮突出显示(选择)文本小部件中的文本?

python - 使用 POX 创建 TCP 数据包

python - 回答完所有问题后无法中断游戏

python - 在 Tkinter 中回调字体

python - Tkinter OptionMenu 可移植消息框

python - 如何在 tkinter 窗口中显示视频预览

python - tktable 中的单元格为空白而不显示内容

python - Google App Engine 开发服务器中的 PyCrypto "ImportError: cannot import name blockalgo"

python - 在函数调用中混合 *vargs 和 **kargs 参数