Tkinter problem

Newt newt_e at blueyonder.co.uk
Fri May 16 10:25:51 EDT 2003


"Eric Brunel" <eric.brunel at pragmadev.com> wrote in message
news:ba2oum$3bs$1 at news-reader11.wanadoo.fr...
> Newt wrote:
> > I've got a Label, created like:
> >
> >         self.sp = Label(self.sel_s, text="")
> >         self.sp.grid(row=4, col=3)
> >
> > I can change the text using:
> >
> >         self.sp.configure(text='Something else')
> >
> > I can conditionally test the text in the label using
> >
> >         if self.sp['text'] == "" :
> >
> > but how do I get the text data out again? I've tried the following ways:
> >
> >         sp = self.sp.cget('text')
> >         sp = self.sp.cget(text)
> >         sp = self.sp['text']
> >
> > but all I get back are errors similar to the below (that ones for sp =
> > self.sp['text'] ):
> >
> > Exception in Tkinter callback
> > Traceback (most recent call last):
> >   File "C:\Python22\lib\lib-tk\Tkinter.py", line 1292, in __call__
> >     return apply(self.func, args)
> >   File "D:\py_stuff\curry.py", line 14, in __call__
> >     return self.fun(*(self.pending + args), **kw)
> >   File "D:\py_stuff\hometown.py", line 121, in bexit
> >     sp = self.sp['text']
> >   File "C:\Python22\lib\lib-tk\Tkinter.py", line 1090, in cget
> >     return self.tk.call(self._w, 'cget', '-' + key)
> > TclError: invalid command name ".9722920.9736352"
> >
> > I'm kinda confused as to what's going on. Any suggestions would be
useful.
> >
> > TIA
> > Newt
>
> Let me guess: when you do the self.sp['text'] or self.sp.cget('text'),
your
> label is not displayed anymore, is it? If it isn't, the behaviour you get
is
> normal: you can't ask a property on a widget that is no more displayed,
since
> this means that the widget no more exists for tk, and therefore cannot
have any
> property...
>
> One way to do what you want is to use tk variables. These variables are a
means
> to store the text or state of a widget outside the widget itself. It is
> typically what you will use for entries, but they can be used for labels
too.
> For example:
>
> self.spVar = StringVar()
> self.spVar.set("")
> self.sp = Label(self.sel_s, textvariable=self.spVar)
> self.sp.grid(row=4, col=3)
>
> Then you can access the text of the label via self.spVar.get(), whether
the
> label is displayed or not.
>
> HTH
> --
> - Eric Brunel <eric.brunel at pragmadev.com> -
> PragmaDev : Real Time Software Development Tools -
http://www.pragmadev.com
>

Doh. I've move the lines so that I destroy my from after grabbing the
variables and it all works.

Thanks alot

Newt






More information about the Python-list mailing list