bind keys

Matthew Dixon Cowles matt at mondoinfo.com
Sun Jun 10 13:45:07 EDT 2001


On Sun, 10 Jun 2001 14:43:09 +0200, Gorny <gorny at hobbiton.org> wrote:

>How do I bind keys to my menubaroptions with TkinteR? I can't figure
>out anything on the web. Nowhere is a good tutorial with that
>informatioin in it..

Gorny,
It turns out that in order to make Tkinter menus respond to alt-keys,
you need to create them with the somewhat unintuitive option,
underline=0. There's an example of that in the Demo directory of the
standard distribution, specifically in:

Demo/tkinter/matt/menu-simple.py

and I'll append another in case you don't have that handy. But I
suspect that you might find using the menu widgets from Pmw (Python
megawidgets) easier. I certainly do. Pmw is at:

http://pmw.sourceforge.net/

Regards,
Matt


from Tkinter import *
import sys

class mainWin:

  def __init__(self,root):
    myMenubar=Menu(root)
    myMenu=Menu(myMenubar)
    myMenu.add_command(label="Do something",
      command=self.doSomething,underline=0)
    myMenu.add_command(label="Quit",command=self.quit,underline=0)
    root.config(menu=myMenubar)
    myMenubar.add_cascade(label="Foo",menu=myMenu,underline=0)
    return None

  def doSomething(self):
    print "foo"
    return None

  def quit(self):
    sys.exit(0)

def main():
  root=Tk()
  mainWin(root)
  root.mainloop()

if __name__=="__main__":
  main()



More information about the Python-list mailing list