Question: How to Prevent Tkinter Menu from Taking Keyboard Focus

galyle galyle at gmail.com
Mon Oct 3 19:24:11 EDT 2011


On Oct 3, 2:40 pm, rantingrick <rantingr... at gmail.com> wrote:
> On Oct 3, 2:55 pm, galyle <gal... at gmail.com> wrote:
>
> > Hello, I'm trying to build a menu which provides suggestions to a user
> > based on input to an entry.  I have done something like this before
> > using Tcl/Tk, so I expected that it would work without much difficulty
> > with Tkinter.  I was wrong.
>
> Why not just use the Tix.ComboBox instead? I believe it's editable.

Thanks for the suggestion.  I tried using a Pmw.ComboBox, but I've run
into essentially the same problem.  If I bring up the selection list
on a keypress, then the selection takes the focus and further input no
longer goes to the entry of the ComboBox.  However, if I set the focus
to the entry after bringing up the selection list, I can at least
continue inputting to the entry, even though the selection list menu
gets placed behind the entry.  This is not desirable behavior, but at
least it is closer.  Any idea how to bring the menu to the forefront
without the entry losing focus?  The following demo demonstrates the
issue:


import Tkinter
import Pmw

class demo:
    def __init__(self, parent):
        self._search_bar = Pmw.ComboBox(parent,
            label_text = 'Register Mnemonic',
            labelpos = 'w', entry_width = 60)
        self._search_bar.pack(padx = 20, pady = 20)
        self._search_bar._entryfield.component('entry').bind('<Key>',
self._suggest_text, add = '+')

    def _suggest_text(self, event):
        curr = self._search_bar._entryfield.getvalue() +
repr(event.char)[1]
        self._search_bar._list.setlist([curr + '_0', curr + '_1', curr
+ '_2'])
        self._search_bar._postList(event)
        self._search_bar._entryfield.component('entry').focus_set()
        print curr

if __name__ == '__main__':
    root = Tkinter.Tk()
    root.title('A Problem')
    demo(root)
    root.mainloop()



More information about the Python-list mailing list