Python Forum
Binding Complex data to Combobox - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Binding Complex data to Combobox (/thread-29638.html)



Binding Complex data to Combobox - gcfernando - Sep-14-2020

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.


RE: Binding Complex data to Combobox - Larz60+ - Sep-14-2020

this is pretty vague, please show code, this would be very helpful
which GUI?


RE: Binding Complex data to Combobox - gcfernando - Sep-14-2020

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