Reputation: 547
I want to have a list of values that is updated when I edit the text on the upper text field of the ComboBox, but I have found no option to access the value of this text field.
I came up with a kind of ridiculous solution, consisting in an Entry Box stamped over the ComboBox header, so I can edit the Entry Box and bind it to get its value to update the ComboBox.
How can I do this better?
import tkinter as tk
from tkinter import ttk
def Run(size=(300,100)):
a_set=('one','two','three')
master = tk.Tk()
master.geometry(str(size[0])+'x'+str(size[1]))
lista=tk.ttk.Combobox(master,width=22,values=a_set)
lista.grid(row=0,column=0)
def update(event):
a=event.widget.get()
newvalues=[i for i in a_set if a in i]
lista['values']=newvalues
entry=tk.Entry(master)
entry.bind('<KeyRelease>',update)
entry.grid(row=0,column=0)
master.mainloop()
Run()
Upvotes: 0
Views: 2022
Reputation: 3079
You can use the textvariable option of the combobox
.
import tkinter as tk
from tkinter import ttk
def update(*args):
newvalues=[i for i in a_set if var.get() in i]
lista['values']=newvalues
a_set=('one','two','three')
master = tk.Tk()
var = tk.StringVar()
var.trace('w', update)
lista = ttk.Combobox(master, width=22, textvariable=var)
lista.grid(row=0,column=0)
master.mainloop()
Upvotes: 2
Reputation:
Here. I think you are trying to implement autocompletion. For ttk
, you have to import it separately.
import tkinter as tk
from tkinter import ttk
def Run(size=(300,100)):
a_set=('one','two','three')
master = tk.Tk()
master.geometry(str(size[0])+'x'+str(size[1]))
lista=ttk.Combobox(master,width=22,values=a_set)
lista.grid(row=1,column=0)
def update(event):
a=event.widget.get().lower()
newvalues=[i for i in a_set if a in i]
lista['values']=newvalues
entry=tk.Entry(master)
entry.bind('<KeyRelease>',update)
entry.grid(row=0,column=0)
master.mainloop()
Run()
Upvotes: 1