From 85935853728834a3b0602da3b284759b80fcd483 Mon Sep 17 00:00:00 2001 From: emkael Date: Fri, 7 Jun 2019 12:53:50 +0200 Subject: Changed DB selection field to custom text with autocomplete --- jfr_playoff/gui/frames/team.py | 57 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 8 deletions(-) (limited to 'jfr_playoff/gui') diff --git a/jfr_playoff/gui/frames/team.py b/jfr_playoff/gui/frames/team.py index c446d05..f1ce7d1 100644 --- a/jfr_playoff/gui/frames/team.py +++ b/jfr_playoff/gui/frames/team.py @@ -131,15 +131,56 @@ class TeamSelectionButton(ttk.Button): selectionFrame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5) -class DBSelectionField(ttk.OptionMenu): +class DBSelectionField(ttk.Entry): + def __init__(self, master, variable, value, *options, **kwargs): + kwargs['textvariable'] = variable + ttk.Entry.__init__(self, master, **kwargs) + self._variable = variable + self._variable.set(value) + self._optionDict = options if options is not None else [] + self.bind('', self._onPress) + self.bind('', self._onChange) + self._matches = [] + self._prevValue = None + def setOptions(self, values): - if self._variable.get() not in values: - self._variable.set('') - menu = self['menu'] - menu.delete(0, tk.END) - for item in values: - menu.add_command( - label=item, command=tk._setit(self._variable, item)) + self._optionDict = values + + def _onPress(self, event): + if event.keysym == 'Tab': + try: + suggestion = self.selection_get() + if len(suggestion) > 0: + prefix = self._variable.get()[0:-len(suggestion)] + phrase = prefix + suggestion + next_suggestion = self._matches[ + (self._matches.index(phrase)+1) % len(self._matches)] + prev_suggestion = self._matches[ + (self._matches.index(phrase)-1) % len(self._matches)] + new_suggestion = prev_suggestion if event.state & 1 \ + else next_suggestion + self.delete(0, tk.END) + self.insert(0, new_suggestion) + self.selection_range(len(prefix), tk.END) + return 'break' + except tk.TclError: + # no text selection + pass + + def _onChange(self, event): + if self._prevValue == self._variable.get() or event.keysym == 'Tab': + return + self._prevValue = self._variable.get() + prefix = self._variable.get() + if len(prefix) > 0: + matches = [d for d in self._optionDict if d.startswith(prefix)] + if len(matches) > 0: + self._matches = matches + text_to_add = matches[0][len(prefix):] + self.insert(tk.END, text_to_add) + self.selection_range(len(prefix), tk.END) + return + self._matches = [] class TeamFetchSettingsFrame(GuiFrame): SOURCE_LINK = 0 -- cgit v1.2.3