3

I would like to be able to create a dropdown list, and assign each choice in the list a different variable. When the option is selected from the dropdown, I would like an entry box to display the associated variable, not the name of the option.

In my program a dropdown list contains a list of people. Each person is a different age. How do I select the person using the dropdown and then have their age display in the entry box?

As you can see below I have entered the choices in this format name*age. I was hoping to try and split by the * and then select the age. I tried specifying the text of the entry box using: text = ((var.get()).split('*')[1]) but this did not work.

Please let me know if there is a way to achieve this.

from Tkinter import *
import Tkinter as ttk 
from ttk import *

root = Tk()
root.title("Age Selector")

mainframe = Frame(root)                                 
mainframe.grid(column=0,row=0, sticky=(N,W,E,S) )
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
mainframe.pack(pady = 10, padx = 10)

var = StringVar(root)

var.set('Bob')

choices = ['Bob*35', 'Garry*45', 'John*32', 'Hank*65','Tyrone*21']

option = OptionMenu(mainframe, var, *choices)

option.grid(row = 1, column =1)

Label(mainframe, text="Age").grid(row = 2, column = 1)

age = StringVar()
age_ent = Entry(mainframe, text = var, width = 15).grid(column =      2, row = 2)

root.mainloop()

1 Answer 1

3

Try following code. Read a comment I added.

from Tkinter import *
import Tkinter as ttk 
from ttk import *

root = Tk()
root.title("Age Selector")

mainframe = Frame(root)                                 
mainframe.grid(column=0,row=0, sticky=(N,W,E,S) )
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
mainframe.pack(pady = 10, padx = 10)

var = StringVar(root)

# Use dictionary to map names to ages.
choices = {
    'Bob': '35',
    'Garry': '45',
    'John': '32',
    'Hank': '64',
    'Tyrone': '21',
}

option = OptionMenu(mainframe, var, *choices)
var.set('Bob')

option.grid(row = 1, column =1)

Label(mainframe, text="Age").grid(row = 2, column = 1)

age = StringVar()
# Bind age instead of var
age_ent = Entry(mainframe, text=age, width = 15).grid(column = 2, row = 2)

# change_age is called on var change.
def change_age(*args):
    age_ = choices[var.get()]
    age.set(age_)
# trace the change of var
var.trace('w', change_age)

root.mainloop()

According to the documentation:

trace(mode, callback) => string

Add a variable observer. Returns the internal name of the observer (you can use this to unregister the observer; see below).

The mode argument is one of “r” (call observer when variable is read by someone), “w” (call when variable is written by someone), or “u” (undefine; call when the variable is deleted).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks very much for this, is there anyway that I can display the name without the *age in the dropdown list also? I also do not understand how the code knows which part is the age and which part is the name. If you could explain I would very much appreciate it. Thanks
@allankey, I updated the code. used a dictionary to names to ages.
Genius, thanks very much for your help! I just had to change the dictionary lookup because there is no longer a need to split the returned value.
@allankey, I saw the mark wrong. I think i wrote comment to wrong answer. I am sorry. Deleting my previous comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.