Tkinter and PMW - dynamic labels

U

uri bushey

Is there an easy way to have a label read from a variable, and update
itself when the variable is changed?

I have a dialogue that asks for a name, and a label that displays the
name that is placed in a variable.

I have the label, and I have a Python megawidget (pmw) PromptDialog that
gets a value from the user. I would like that value to update the label
- is there a way to do that without creating and destroying the label in
a function that is called every time the PromptDialog is called?

Thanks!
 
C

Christopher T King

Is there an easy way to have a label read from a variable, and update
itself when the variable is changed?

You can do this by using a StringVar. Try this in an interactive session:

IntVar and DoubleVar provide similar functionality for ints and floats,
respectively. Other widgets provide a similar 'variable' option to allow
them to directly interact with variables.

Unfortunately the Var classes don't play well with normal Python
operations; you must always access them through their .set() and .get()
methods.
I have the label, and I have a Python megawidget (pmw) PromptDialog that
gets a value from the user. I would like that value to update the label
- is there a way to do that without creating and destroying the label in
a function that is called every time the PromptDialog is called?

Yes, you can simply change the label's text attribute:

mylabel['text'] = newvalue

Hope this helps.
 
U

uri bushey

You can do this by using a StringVar. Try this in an interactive session:

Yes, you can simply change the label's text attribute:

mylabel['text'] = newvalue

Hope this helps.

Yes, thank you so much. This turned out to be the most elegant solution
I think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top