[Tutor] The what and why of StringVar()
Tue, 22 Feb 2000 22:15:28 +0900
I am wondering if someone can help explain "StringVar()" to me. I was
trying to write a simple program using Tkinter to take input from an Entry
widget and then use a button command to pass it off to a function to do
something. But I was unsuccessful in accessing the variable attached to the
Entry widget itself. (I still don't understand why.)
After poking about in the "DEMO's" Folder I found a demo in the "Matt"
folder that allowed me to get around this problem by using StringVar().
However I don't understand why this is used, how it works or why I need it.
So far I can't find any documentation either on line of in my 3 Python
books (hope I didn't stupidly overlook something). Seeing as how this is a
necessary way to access the Entry widget variable I would like to
understand it. I thought that perhaps this method is part of the string
module (since Matt's script imports that module, and dir(string) shows
StringVar being a method) but it works without importing the string module.
Any help would be appreciated. I will copy Matt's example script below
minus comments so that you can see what I am talking about.
-----Matt's entry-with-shared-variable.py script-----
from Tkinter import *
# This program shows how to make a typein box shadow a program variable.
def __init__(self, master=None):
self.entrythingy = Entry(self)
self.button = Button(self, text="Uppercase The Entry",
self.contents = StringVar()
self.contents.set("this is a variable")
str = string.upper(self.contents.get())
def print_contents(self, event):
print "hi. contents of entry is now ---->", self.contents.get()
root = App()