seit Kurzem beschäftige ich mich mit der ComboBox von tkinter.ttk.
Ich versuche eine Lösung zu finden, bei der im Ausgabefeld der ComboBox, die Eingabe des Zeichens überprüft und dann gleichzeitig das Ein- Ausgabefeld der Combobox, mit einem in der Liste vorkommenden Ergebnis ausgefüllt wird.
Teilweise ist mir das gelungen, jedoch bleibt immer die erste Eingabe eines Zeichens zuerst nicht berücksichtigt.
Ich poste hier mal meinen Testcode:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# For Python3.x
import tkinter as tk
import tkinter.ttk as ttk
def action(event):
"""
a combo box item has been selected, do some action
"""
myentry = combo.get()
if not myentry:
return event.keysym
print('0', myentry)
sign_count = len(myentry)
while not [combo.set(x) for x in L
if x[:sign_count+1].startswith(myentry)]:
combo.set('')
return
combo.set(combo.get())
print('1', combo.get())
def output(event):
combo.set(combo.get()[1:])
print('Result: ', combo.get())
my_win = tk.Tk()
frame = tk.Frame(my_win)
frame.pack(side='top', expand=True)
# create the combo box
combo = ttk.Combobox()
combo.bind('<<ComboboxSelected>>', action)
combo.bind('<KeyPress>', action)
combo.bind('<Return>', output)
combo.focus_set()
L = ['aaaaaa', 'abbbbb', 'aaabbb', 'acabbb', 'bbbbbb', 'cccccc',
'vvvvvv', 'wwwwww', 'xxxxxx', 'yyyyyy', 'yywyyy', 'zzzzzz', ]
# load the combo box with the list
combo['values'] = L
# pack the widgets vertically in this order
#entry.pack(fill='both', expand='yes')
combo.pack()
my_win.mainloop()
Grüße Nobuddy