Retrieval of widget property values

J

Jah_Alarm

hi, here's my problem:

let's say I have a function that uses some widget's property. How canI
retireve the value of this property?

For example,

PopSize=IntVar();

popsize=Entry(root,width=10,textvariable=PopSize)

def rand_opo_gen (self,event):
popsize_start=#need to get the textvariable value from Entry
popsize, not variable PopSize!
pop1=random.random(popsize_start*2)

Another option, of course, is that the variable PopSize acquires the
value from Entry, but I can't see how to do it either

cheers,

alex
 
P

Peter Otten

Jah_Alarm said:
hi, here's my problem:

let's say I have a function that uses some widget's property. How canI
retireve the value of this property?

For example,

PopSize=IntVar();

popsize=Entry(root,width=10,textvariable=PopSize)

def rand_opo_gen (self,event):
popsize_start=#need to get the textvariable value from Entry
popsize, not variable PopSize!
pop1=random.random(popsize_start*2)

Another option, of course, is that the variable PopSize acquires the
value from Entry, but I can't see how to do it either

In Tkinter you normally access properties of a widget with widget[propname].
This doesn't help here because it will only give you a name made up by
Tkinter instead of the actual IntVar instance.

Therefore the easiest approach is to keep a reference to the IntVar instance
and invoke IntVar.get()/set(new_value). A self-contained example:

import Tkinter as tk

root = tk.Tk()
size_var = tk.IntVar()
size_var.set(42)

size_entry = tk.Entry(root, textvariable=size_var)
size_entry.pack()

def show_size():
print size_var.get()

show_button = tk.Button(root, text="show size", command=show_size)
show_button.pack()

def times_two():
new_value = size_var.get()*2
size_var.set(new_value)

timestwo_button = tk.Button(root, text="times two", command=times_two)
timestwo_button.pack()

root.mainloop()

Peter
 
A

Alex

thanks Peter,

I'm probably not getting something, but how do show_size and
show_button account for the changes in Entry? There are no references
to the widget in either the function of the button. Sorry if the
question is too dumb.

cheers,

Alex

Jah_Alarm said:
hi, here's my problem:
let's say I have a function that uses some widget's property. How canI
retireve the value of this property?
For example,


def rand_opo_gen (self,event):
      popsize_start=#need to get the textvariable value from Entry
popsize, not variable PopSize!
      pop1=random.random(popsize_start*2)
Another option, of course, is that the variable PopSize acquires the
value from Entry, but I can't see how to do it either

In Tkinter you normally access properties of a widget with widget[propname].
This doesn't help here because it will only give you a name made up by
Tkinter instead of the actual IntVar instance.

Therefore the easiest approach is to keep a reference to the IntVar instance
and invoke IntVar.get()/set(new_value). A self-contained example:

import Tkinter as tk

root = tk.Tk()
size_var = tk.IntVar()
size_var.set(42)

size_entry = tk.Entry(root, textvariable=size_var)
size_entry.pack()

def show_size():
    print size_var.get()

show_button = tk.Button(root, text="show size", command=show_size)
show_button.pack()

def times_two():
    new_value = size_var.get()*2
    size_var.set(new_value)

timestwo_button = tk.Button(root, text="times two", command=times_two)
timestwo_button.pack()

root.mainloop()

Peter
 
P

Peter Otten

Alex said:
thanks Peter,

I'm probably not getting something, but how do show_size and
show_button account for the changes in Entry? There are no references
to the widget in either the function of the button. Sorry if the
question is too dumb.

After

size_var = IntVar()
size_entry = Entry(..., textvariable=size_var)

Tkinter automatically synchronizes the value in the Entry with that in the
IntVar. Therefore you can read the value shown in the Entry widget with

size_var.get()

Perhaps it becomes clearer in an interactive session:

The Entry widget should show 42 now instead of the initial 0. Type 12345
into the widget, then go back to the shell:
12345

Peter
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top