Tkinter: Changing a variable within a function
Question: I know this kind of question gets asked all the time but either i've been
unable to come across the answer i need, or i've been unable to understand it
when i did.

I want to be able to do something like:

    
    
    spam = StringVar()
    spam.set(aValue)
    class MyScale(Scale):
        def __init__(self,var,*args,**kwargs):
            Scale.__init__(self,*args,**kwargs)
            self.bind("<ButtonRelease-1>",self.getValue)
            self.set(var.get())
        def getValue(self,event):
            ## spam gets changed to the new value set 
            ## by the user manipulating the scale
            var.set(self.get)
    eggs = MyScale(spam,*args,**kwargs)
    eggs.pack()
    

Of course, i get back "NameError: global name 'var' is not defined."

How do i get around the inability to pass arguments to getValue? I've been
warned against using global variables but is that my only option? Is it
setting up a separate scale class for each variable i want to change? I get
the feeling i'm missing something thats right under my nose...

edit: is this what you mean?

    
    
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
        return self.func(*args)
      File "C:\...\interface.py", line 70, in getValue
        var.set(self.get)
    NameError: global name 'var' is not defined
    

Sorry, I've only been programming a month and some of the jargon still escapes
me.


Answer: Please give this a shot.

Lots of example code out there generously uses globals, like your "var"
variable.

I have used your var argument to act as a pointer back to the original spam
object; assigned to self.var_pointer within the MyScale class.

The code below will change the value of 'spam' (and 'eggs') on the scale's
ButtonRelease.

You can check out the value by typing eggs.get() or spam.get() to see the
changed value.

    
    
    from Tkinter import *
    root = Tk()
    
    aValue = "5"
    spam = StringVar()
    spam.set(aValue)
    
    class MyScale(Scale):
        def __init__(self,var,*args,**kwargs):
            self.var_pointer = var
            Scale.__init__(self,*args,**kwargs)
            self.bind("<ButtonRelease-1>",self.getValue)
            self.set(var.get())
        def getValue(self,event):
            ## spam gets changed to the new value set 
            ## by the user manipulating the scale
            self.var_pointer.set(self.get())
    
    eggs = MyScale(spam)
    eggs.pack(anchor=CENTER)
    

