bind keys 
Author Message
 bind keys

Hi,

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..

Anyone?

Gegroet,
Gorny



Thu, 27 Nov 2003 20:43:09 GMT  
 bind keys

Quote:

>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()



Fri, 28 Nov 2003 01:45:07 GMT  
 bind keys
That's not what I ment exactly because I did that already.
Example: In most editors you have an shortcut to
File --> Open file --> by pressing
Control -O you imediately open the fileselectionscreen.

How do I set these Control-O events so if I press Conrtol-O
in my own program it'll open the fileselectionscreen?

Greetz,
Gorny

--
Never trust anything that bleeds for 3-5 days and doesn't die...
http://gorny.cjb.net



Fri, 28 Nov 2003 03:14:05 GMT  
 bind keys
Gorny,

Quote:
> That's not what I ment exactly because I did that already.
> Example: In most editors you have an shortcut to
> File --> Open file --> by pressing
> Control -O you imediately open the fileselectionscreen.

> How do I set these Control-O events so if I press Conrtol-O
> in my own program it'll open the fileselectionscreen?

Ah yes, that's a little different. I don't think Tkinter supports
doing that directly but it is possible to do. The trick is to have
your main window accept the key event and act on it.

Here's an example of that. There's more detail in Fredrik Lundh's
excellent An Introduction to Tkinter. This page (wrapped for line
length) may be a good start:

http://www.pythonware.com/library/tkinter/
  introduction/events-and-bindings.htm

Regards,
Matt

from Tkinter import *
import sys

class mainWin:

  def __init__(self,root):
    root.bind("<Control-d>",self.doSomethingKey)
    root.bind("<Control-q>",self.quitKey)
    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 doSomethingKey(self,event):
    self.doSomething()
    return "break" # No further processing of this keystroke

  def quitKey(self,event):
    self.quit()

  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()



Fri, 28 Nov 2003 03:32:22 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. binding keys to slider in Tkinter?

2. bind key in Tkinter

3. Defining modifier binding keys

4. tk: howto bind key combinations

5. Bind keys to access functions directly

6. help with scrolledframe and binding keys

7. Problems Binding keys to buttons?

8. binding keys

9. Binding keys to buttons in Tcl/Tk 7.4/4.0

10. Binding keys to buttons in Tcl/Tk 7.4/4.0

11. tkinter: Passing arguments when binding keys to functions

12. binding keys to scale

 

 
Powered by phpBB® Forum Software