Tk: How would I add an accelerator on a button/label in tkinter
morden
morden at shadows.net
Mon Jan 13 21:33:23 EST 2003
Derk Gwen wrote:
> # How could I make the button pressed when user presses Alt-P
> # on the keyboard? I saw accelerators on menus in Tk screenshots
> # found on the web but I haven't seen any on standalone buttons.
> # Is there any way to get those?
>
> All a menu accelerator is is a comment on the displayed menu. To actually
> implement the accelerator, you bind the key sequence to a command.
Is there a platform independent way of doing this or I should
use X11 specific commands for this?
> This
> binding is independent of whether there is button or menu that does the
> same thing.
In gui8.py example accelerators are set with unerline=X command:
#!/usr/local/bin/python
from Tkinter import * # get widget classes
from Dialog import Dialog
class Hello(Frame): # an extended frame
def __init__(self, parent=None): # attach to top-level?
Frame.__init__(self, parent) # do superclass init
self.pack()
self.createWidgets() # attach frames/widgets
self.master.title("Buttons and Menus") # set window-manager
info
self.master.iconname("tkpython") # label when iconified
def createWidgets(self):
self.makeMenuBar()
Label(self, text='Hello menu/toolbar world').pack(padx=30, pady=30)
self.makeToolBar()
def makeToolBar(self):
toolbar = Frame(self, cursor='hand2', relief=SUNKEN, bd=2)
toolbar.pack(side=BOTTOM, fill=X)
Button(toolbar, text='Quit', command=self.quit
).pack(side=RIGHT)
Button(toolbar, text='Hello',
command=self.greeting).pack(side=LEFT)
def makeMenuBar(self):
self.menubar = Frame(self, relief=RAISED, bd=2)
self.menubar.pack(side=TOP, fill=X)
self.fileMenu()
self.editMenu()
def fileMenu(self):
mbutton = Menubutton(self.menubar, text='File', underline=0)
mbutton.pack(side=LEFT)
menu = Menu(mbutton)
menu.add_command(label='New...', command=self.notdone)
menu.add_command(label='Open...', command=self.notdone)
menu.add_command(label='Quit', command=self.quit, underline=0)
mbutton['menu'] = menu
return mbutton
def editMenu(self):
mbutton = Menubutton(self.menubar, text='Edit', underline=0)
mbutton.pack(side=LEFT)
menu = Menu(mbutton)
menu.add_command(label='Cut', command=self.notdone)
menu.add_command(label='Paste', command=self.notdone)
menu.add_separator({})
submenu = Menu(menu)
submenu.add_command(label='Spam', command=self.notdone)
submenu.add_command(label='Eggs', command=self.greeting)
menu.add_cascade(label='Stuff', menu=submenu)
menu.add_command(label='Delete', command=self.greeting)
menu.entryconfig(2, state=DISABLED)
mbutton['menu'] = menu
return mbutton
def greeting(self):
Dialog(self, title = 'greeting',
text = 'Howdy',
bitmap = '', default=0, strings=('hi',))
def notdone(self):
Dialog(self, title = 'Not implemented',
text = 'Not yet available',
bitmap = 'error', default=0, strings=('OK',))
def quit(self):
ans = Dialog(self, title = 'Verify quit',
text = 'Are you sure you want to quit?',
bitmap = 'question',
default = 1,
strings = ('Yes', 'No'))
if ans.num == 0: Frame.quit(self)
if __name__ == '__main__': Hello().mainloop() # if I'm run as a script
Alt-F works to get File submenu!
However when I tried to modify driver in Dialog.py (from Tkinter?):
if __name__ == '__main__':
t = Button(None, {'text': 'Test',
'command': _test,
Pack: {}}, underline=1)
q = Button(None, {'text': 'Quit',
'command': t.quit,
Pack: {}})
t.mainloop()
Underline was drawn under 'e' in Test
but Alt-e did not activate the button. I've checked code in Dialog.py
but there does not seem to be handling for Alt-? sequences marked by
underlines. Where could I look further?
Thank you!
More information about the Python-list
mailing list