[Tkinter-discuss] Get value instead of PY_VAR

Michael O'Donnell michael.odonnell at uam.es
Thu Nov 14 17:38:32 CET 2013


Hi Kevin,

  Try instead:

    self.defaults['copytext'] = self.copytext.get()

the get() method returns the value of the StringVar.

Mick




On 14 November 2013 17:03, Kevin Walzer <kw at codebykevin.com> wrote:
> I'm going around in circles trying to serialize some app configuration data
> in my Tkinter app via pickle, and I'd appreciate some help.
>
> I set various app configurations via checkboxes and StringVar(). Sometimes
> the value of one of the checkboxes is lost and when I try to print the
> value, I get PY_VAR#2 instead. Here's a relevant snippet setting the values:
>
>         self.copytext= StringVar()
>         self.copytext.set(self.defaults['copytext'])
>
>        self.whoischeck = Tile.Checkbutton(self.whoisframe,
> variable=self.whois, text='Display Raw Whois Output')
>
> And here's the snippet that retrieves the value:
>
>     self.defaults['copytext'] = self.copytext
>
> Using this call, printing the value of self.defaults['copytext'] results in
> PY_VAR#2, which doesn't actually retain the value of the var I'm setting (1
> or 0). It also results in a error when I try to serialize the data, because
> pickle can't handle this type of data:
>
>  Can't pickle 'tkapp' object: <tkapp object at 0x01FCB090>
>
> I can actually get the correct value if I change one call as follows:
>
> self.defaults['copytext'] = self.copytext.get()
>
> But then, trying to pickle this value, I get this error:
>
> 'str' object has no attribute 'get'
>
> What's the right way in Tkinter to get the value of a StringVar() from a
> checkbutton, and then serialize the value via pickle?
>
> Sometimes I can get this to work, but I don't understand the underlying
> dynamics of it; the intersection of Python objects and Tcl variables gives
> me headaches. I am especially unclear why I get PY_VAR rather than the value
> in some instances, but not others.
>
> Thanks,
> Kevin
>
> --
> Kevin Walzer
> Code by Kevin/Mobile Code by Kevin
> http://www.codebykevin.com
> http://www.wtmobilesoftware.com
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> https://mail.python.org/mailman/listinfo/tkinter-discuss


More information about the Tkinter-discuss mailing list