Question: How to Prevent Tkinter Menu from Taking Keyboard Focus
woooee
woooee at gmail.com
Tue Oct 4 11:45:10 EDT 2011
Sorry, I did not understand the question correctly, and so have added
another focus_set for the entry after the menu's creation. You can
still enter after the menu comes up, even though you can't see where
you are entering.
import Tkinter
class demo:
def __init__(self, parent):
self._entry = Tkinter.Entry(width = 60)
self._suggestions = Tkinter.Menu(parent, tearoff = 0,
takefocus = 0)
self._entry.pack(padx = 20, pady = 20)
self._entry.bind('<Key>', self._suggest_text, add = '+')
self._entry.focus_set()
def _suggest_text(self, event):
curr = self._entry.get() + repr(event.char)[1]
x = self._entry.winfo_rootx()
y = self._entry.winfo_rooty()
y += self._entry.winfo_height()
try:
self._suggestions.delete(0, 'end')
self._suggestions.add_command(label = curr + '_1')
self._suggestions.add_command(label = curr + '_2')
self._suggestions.add_command(label = curr + '_3')
self._suggestions.add_command(label = curr + '_4')
self._suggestions.post(x, y)
finally:
self._suggestions.grab_release()
self._entry.focus_set()
if __name__ == '__main__':
root = Tkinter.Tk()
root.title('A Problem')
demo(root)
root.mainloop()
More information about the Python-list
mailing list