bind keys
Matthew Dixon Cowles
matt at mondoinfo.com
Sun Jun 10 15:32:22 EDT 2001
Gorny,
> 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()
More information about the Python-list
mailing list