python Tk在我设置后保留标签文本 [英] python Tk keeping label text after I set

查看:29
本文介绍了python Tk在我设置后保留标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的 Python Tk 程序.我似乎无法阻止一个简单的问题,而且我确信我遗漏了一些简单的东西.

It's an extremely simple Python Tk program. I can't seem to stop a simple problem and I am sure I am missing something easy.

我做了一个标签:

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

稍后在同一个程序中,我想将该标签更新为Foo"...

Later in the same program I want to update that label to say "Foo"...

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

标签现在看起来像Fooething有点长"目标是只有Foo"并清除标签文本的其余部分.

The label now looks like "Fooething kinda long" The goal would be to just have "Foo" and clear the rest of the label text.

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

I tried to set the label to a long string of spaces but for some reason that's not clearing the text in that field. What is the right way to do this?

这里有一个完整的例子来说明我的问题.

Here is a complete example that demonstrates my problem.

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()

推荐答案

你首先要考虑做不同的事情是分离:

The first thing you will want to consider doing differently is separating:

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

进入

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

请阅读→此链接← 有关这方面的信息.

Please read →this link← for information concerning this.

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

To update a Label's text you do not need a control variable.
You can change the text in one of two ways:

myLabel.configure(text='new text')

myLabel['text'] = 'new text'

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

You can inspect the second example →here←
to further your understanding of control variables in regard to this topic.

update_idletasks() 不需要更新标签小部件的文本.
我只发现在使用窗口的 几何.
一个例子是将窗口居中.

基于

将根和应用程序创建为全局函数的局部变量是非常非正统的.
根据→此处←的结构尝试重写.

Creating your root and app as local variables of a global function is very unorthodox.
Try a rewrite based on the structure seen →here←.

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

You will want to avoid using time.sleep() with a tkinter app; because it's in conflict with the event loop. Try splitting your single method into multiple methods, and then you can use Tk's after(milliseconds, method) method at the end of each to gracefully move to the next, after x milliseconds.

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

I'd also consider removing the return from your method.

这篇关于python Tk在我设置后保留标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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