Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

python - How do widgets update in Tkinter?

Okay, so I'm just trying to get some clarification on why my code is not working like I thought it would.

I am building a GUI, and I want to display text on a Label with a text variable. I have already made a function that updates the Label when the function is called, but of course that is not my problem.

My problem stems from me trying to implement a "print one letter at a time" type of label. While it prints to the terminal in the way I want it to, the label widget only updates after the whole function has finished (visually its the same as just printing the whole string instead of printing a letter at a time).

So what am I missing, what do I not understand? Can you guys help me? Let me post some code so you guys can see where my error is.

I tried both of these independently and they both game me the same result, which was not what I desired.

def feeder(phrase):
    """Takes a string and displays the content like video game dialog."""
    message = ""
    for letter in phrase:       
        time.sleep(.15)
        message += letter
        information.set(message)
        #print message

def feeder2(phrase):
    """Same as feeder, but trying out recursion"""
    current.index += 1
    if current.index <= len(phrase):
        information.set(phrase[:current.index])
        time.sleep(.1)
        feeder2(current.status())

I'm not positive if I need to post more code, so you guys can understand better, but if thats the case, I will do that.

Those 2 functions are used in this function

def get_info():
    """This function sets the textvariable information."""
    #information.set(current)
    feeder2(current.status())

Which in turn is used in this function

def validate():
    """ This function checks our guess and keeps track of our statistics for us. This is the function run when we press the enter button. """
    current.turn += 1
    if entry.get() == current.name:
        if entry.get() == "clearing":
                print "Not quite, but lets try again."
                current.guesses -= 1
        if entry.get() != "clearing":
            print "Great Guess!"
            current.points += 1

    else:
        print "Not quite, but lets try again."
        current.guesses -= 1
    print current
    get_info()
    entry.delete(0, END)
    current.name = "clearing"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The UI will update every time the event loop is entered. This is because painting is done via events (also known as "idle tasks" because they are done when the UI is otherwise idle).

Your problem is this: when you write a loop and do time.sleep, the event loop will not be entered while that loop is running, so no redrawing will occur.

You can solve your problem in at least a couple different ways. For one, you can just call update_idletasks which will refresh the screen. That will solve the repainting, but because you are sleeping the UI will be unresponsive during your loop (since button and key presses aren't "idle tasks").

Another solution is to write a function that takes a string, pulls one character off the string and adds it to the widget. Then it arranges for itself to be called again via the event loop. For example:

import Tkinter as tk

class App(tk.Tk):
    def __init__(self,*args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.label = tk.Label(self, text="", width=20, anchor="w")
        self.label.pack(side="top",fill="both",expand=True)
        self.print_label_slowly("Hello, world!")

    def print_label_slowly(self, message):
        '''Print a label one character at a time using the event loop'''
        t = self.label.cget("text")
        t += message[0]
        self.label.config(text=t)
        if len(message) > 1:
            self.after(500, self.print_label_slowly, message[1:])

app = App()
app.mainloop()

This type of solution guarantees that your UI stays responsive while still running your code in a loop. Only, instead of using an explicit loop you add work to the already running event loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...