Python Forum

Full Version: Binding Complex data to Combobox
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

In Customer table have two columns "ID" and "Name"

I want to bind the Customer information to Combobox, as Values I set Name property.

My question is once I change the index of Combobox, how can I get the ID property value based on the Name.
this is pretty vague, please show code, this would be very helpful
which GUI?
GUI : tkinter

Default Way ....
import tkinter as tk
from tkinter import ttk
 
app = tk.Tk() 
app.geometry('200x100')

labelTop = tk.Label(app,
                    text = "Choose your favourite month")
labelTop.grid(column=0, row=0)

comboExample = ttk.Combobox(app, 
                            values=[
                                    "January", 
                                    "February",
                                    "March",
                                    "April"])
pprint(dict(comboExample)) 
comboExample.grid(column=0, row=1)
comboExample.current(1)

print(comboExample.current(), comboExample.get())

app.mainloop()
Expected ....

import tkinter as tk
from tkinter import ttk
 
app = tk.Tk() 
app.geometry('200x100')

labelTop = tk.Label(app,
                    text = "Choose your favourite month")
labelTop.grid(column=0, row=0)

comboExample = ttk.Combobox(app, 
                            values=[(1,"January"),(2,"February"),(3,"March"),(4,"April")])
pprint(dict(comboExample)) 
comboExample.grid(column=0, row=1)
comboExample.current(1)

print(comboExample.current(), comboExample.get())

app.mainloop()
Question

In C# we use Value Member and Display Member
we Set 1, 2 ... as Value Member
Set January, .... as Display Member

When the application run shows January , .... but when we change the select index we can get value Member easily

So how can we same that in python using tkinter