10

I'm trying to associate a variable with a Tkinter entry widget, in a way that:

  1. Whenever I change the value (the "content") of the entry, mainly by typing something into it, the variable automatically gets assigned the value of what I've typed. Without me having to push a button "Update value " or something like that first.

  2. Whenever the variable gets changed (by some other part of the programm), I want the entry value displayed to be adjusted automatically. I believe that this could work via the textvariable.

I read the example on http://effbot.org/tkinterbook/entry.htm, but it is not exactly helping me for what I have in mind. I have a feeling that there is a way of ensuring the first condition with using entry's "validate". Any ideas?

dfeuer
  • 48,079
  • 5
  • 63
  • 167
Sano98
  • 419
  • 2
  • 7
  • 17
  • 2
    Perhaps you could suggest an alternative? :) – Justin Ethier Mar 26 '10 at 14:42
  • 4
    What's wrong with tkinter? According to the python homepage, it is "...Python's de-facto standard GUI package." And the "most commonly used one." – Sano98 Mar 26 '10 at 15:05
  • Control variables: http://infohost.nmt.edu/tcc/help/pubs/tkinter/control-variables.html – Gary Kerr Mar 26 '10 at 15:16
  • When you say 'variable', do you mean a python variable like a `int` or a `str` or a Tkinter variable like `IntVar` or `StringVar`? – Paul Mar 26 '10 at 15:18
  • http://stackoverflow.com/questions/520015/ contains discussion of GUI toolkits in Python, which would be a good place to discuss the relative merits of these. There is no one advocating Tkinter (and one guy really dissing it), which is not surprising for me; Tk was hip back in the 90s when it was added to Python, but it's sort of lost its luster. It can look okay on Windows, but I've never seen a Python Tkinter app that wasn't hideous on Linux. Notably missing from the thread I linked is PyGTK http://www.pygtk.org/ – these bindings are mature and GTK+ is a decent framework with which to work. – Mike Graham Mar 26 '10 at 15:27
  • The variables i need to work with are normal python ints or strings. But since Tkinter variables can easily be converted via get and set, is it that much of a difference? – Sano98 Mar 26 '10 at 15:28

1 Answers1

15

I think you want something like this. In the example below, I created a variable myvar and assigned it to be textvariable of both a Label and Entry widgets. This way both are coupled and changes in the Entry widget will reflect automatically in Label.

You can also set trace on variables, e.g. to write to stdout.

from tkinter import *


root = Tk()
root.title("MyApp")

myvar = StringVar()

def mywarWritten(*args):
    print "mywarWritten",myvar.get()

myvar.trace("w", mywarWritten)

label = Label(root, textvariable=myvar)
label.pack()

text_entry = Entry(root, textvariable=myvar)
text_entry.pack()

root.mainloop()
nbro
  • 15,395
  • 32
  • 113
  • 196
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • Thank you, this goes in the right direction, but to work with the variables, I need to convert them to normal python variables. Your example works because the widgets share the same textvariable. But what would you do, for example, if you wanted to print the content of your entry to the standard output whenever it gets changed instead of writing it in a tkinter label? – Sano98 Mar 26 '10 at 16:07
  • 1
    You do this: `text_entry.bind('', handler)` where `handler` is a function that assigns your python variable. See my answer. – Paul Mar 26 '10 at 16:30
  • @Sano98, i have edited answer, you can easily trace variables for read/write – Anurag Uniyal Mar 26 '10 at 16:36
  • Thanks, I used your proposal after I encountered the problems described by Brian Oakley when trying it with binding to keyppress as bpowah suggested. After a little study, I now see that tracing seems taylormade for a problem like this. But when using IntVar instead of StringVar, a ValueError occurs when deleting completely the content of the entry - the empty literal "" is not valid for int conversion. It is given the value 0, which is what I expected, but is there a way to avoid the error? Or to catch it somehow? – Sano98 Mar 29 '10 at 08:48
  • @Sano98, you may either change the global getint defined in tkinter or may better derive a class from IntVar to overide get, or may be there are better ways, you may ask another question – Anurag Uniyal Mar 29 '10 at 08:54
  • Please, please, please **please** never use `from import *` in examples. – Fake Name Jun 11 '18 at 21:19