[help]Is this a bug or my code's fault

Eric Brunel eric.brunel at pragmadev.com
Tue Apr 8 08:48:00 EDT 2003


dolephi9080 wrote:
> import Tkinter
> import sys
> import os
> import os.path
> 
> 
> 
> if __name__ == "__main__":
>     rt = Tkinter.Tk()
>     btn = Tkinter.Button(rt,text='one')
>     btn.setvar('v','one')  #<=here I set a var to the btn object
>     with v='one'
>     btn.pack()
>     btn1=Tkinter.Button(rt,text="aa")
>     btn1.setvar('v','aa')   #<=here I set a vat to the btn1 object
>     v='aa'
>     btn1.pack()
>     print btn1.getvar('v') #<= print the btn1 v
>     print btn.getvar('v') #<= print the btn v
> 
>     rt.mainloop()
> 
> 
> 
> 
> please look at the comment, I set the value to two buttons, but when I
> print it at last, two buttons got the same data. I don't know why when I
> set the btn1's value, it will rewrite the btn's value, is this a bug? by
> the way I'm using activestate python 2.2.2

I don't quite understand what you're trying to do; maybe you should start by 
explaining your exact purpose with this code.

I particularly can't understand why you use the setvar method here. AFAIK, this 
method is used to manipulate variables in the tcl/tk layer behind Tkinter. Since 
Python has its own variables, I can't see any reason why you'd want to do this.

And the explanation to your problem is simple: tk has nothing looking like 
attributes on objects, just plain variables. So you don't do what you think you 
do: when doing btn.setvar('v', 'one'), you're setting a tcl/tk variable named v 
to the value "one". This has nothing to do with btn at all. So when you're doing 
btn1.setvar('v', 'aa'), you're modifying the *same* tcl/tk variable v to the 
value "aa".

But please explain what you're trying to do here, so that we can provide more 
useful advices.

HTH however...
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com





More information about the Python-list mailing list