Python ttk.combobox Force post / open

I am trying to extend the combotox ttk class to allow autoplay. the code that works far for me, but I would like it to display the dropdown as soon as the text has been entered without removing focus from the input part of the widget.

The part I am struggling with is finding a way to force the dropdown, in the python docs I cannot find any mention of this, however in the tk docs I found a post method that I suppose should do it, except it seems to be not implemented in python shell.

I also tried to create a down arrow event after the outsource happened, however while this shows the popup it removes focus and tries to set focus after that event doesn't seem to work (focus won't return)

Does anyone know of a function that I can use to achieve this goal?

The code I have is for python 3.3 using standard libraries only:

class AutoCombobox(ttk.Combobox):
    def __init__(self, parent, **options):
        ttk.Combobox.__init__(self, parent, **options)
        self.bind("<KeyRelease>", self.AutoComplete_1)
        self.bind("<<ComboboxSelected>>", self.Cancel_Autocomplete)
        self.bind("<Return>", self.Cancel_Autocomplete)
        self.autoid = None

    def Cancel_Autocomplete(self, event=None):
        self.after_cancel(self.autoid) 

    def AutoComplete_1(self, event):
        if self.autoid != None:
            self.after_cancel(self.autoid)
        if event.keysym in ["BackSpace", "Delete", "Return"]:
            return
        self.autoid = self.after(200, self.AutoComplete_2)

    def AutoComplete_2(self):
        data = self.get()
        if data != "":
            for entry in self["values"]:
                match = True
                try:
                    for index in range(0, len(data)):
                        if data[index] != entry[index]:
                            match = False
                            break
                except IndexError:
                    match = False
                if match == True:
                    self.set(entry)
                    self.selection_range(len(data), "end")
                    self.event_generate("<Down>",when="tail")
                    self.focus_set()
                    break
            self.autoid = None

      

+3


source to share


1 answer


You don't need to inherit ttk.Combobox for this event; just use event_generate to force the dropdown:



box = Combobox(...)
def callback(box):
    box.event_generate('<Down>')

      

0


source







All Articles