Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning variables using choices in TKInter dropdown menu - Python 2.7

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()
like image 419
alkey Avatar asked Jan 30 '26 10:01

alkey


1 Answers

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).

like image 133
falsetru Avatar answered Feb 02 '26 02:02

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!