Jason Woodruff
Jason Woodruff

Reputation: 87

Python, Tkinter - Making get() dynamic

Below is some code that I'm testing with. In this code, I am able to create a window and have a Label on top and a Entry field on bottom. When I type in that entry field, I can dynamically change what's in the label. Now, I have included a function that is trying to evaluate a variable assigned to "tex", by storing what is predefined in the Entry widget. Which is "cat". This is picked up by:

tex = e.get()

I understand that get() is not changing dynamically as I change the text in the entry widget. So it cannot change to "dog" when I change the string in the entry widget. Is this possible? Here is the code:

from Tkinter import *
import time

root = Tk()

def change():   
    if tex  == ("cat"):
        time.sleep(0.5)
        pass
    else:
        time.sleep(0.5)
        e.delete(0, END)
        e.insert(0, "dog")

v = StringVar()

e = Entry(root, textvariable=v)
e.insert(0, "cat")
e.pack(side=BOTTOM)
tex = e.get() #When text is defined with get(), it does not change 
              #dynamically with the entry widget


l = Label(root, textvariable=v)
l.pack(side=TOP)

change()

root.mainloop()

Any help would be appreciated.

Upvotes: 1

Views: 1582

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

To answer your specific question, no, there is no way for tex to magically keep updated when the entry widget changes. That feature is exactly why the textvariable attribute exists -- it causes the variable to always be updated with the value in the entry widget. If you need the data in another variable, you will have to call the .get() method.

That being said, you can set a trace on the variable, or a binding on the entry widget, and have it call code that can update tex to whatever you want when the entry widget changes.

For example, the following code shows how to have callback called whenever the value in the entry widget changes:

def callback(*args):
    global tex
    tex = e.get()
v.trace("w", callback)

For more information about what arguments are passed to the callback, see What are the arguments to Tkinter variable trace method callbacks?

All that being said, you have a couple of critical flaws in your code. First, you are creating the StringVar incorrectly. It needs to be v = StringVar() (note the trailing ()).

Also, you should never call sleep in the main thread of a GUI program. Read up on the after method if you want to execute some code after some time has elapsed.

Upvotes: 3

Related Questions