[Tkinter-discuss] bind <Alt-[1..5]> not working

Michael Lange klappnase at web.de
Mon Sep 16 20:57:03 CEST 2013


Hi,

On Mon, 16 Sep 2013 23:37:26 +0530
Bhaskar Chaudhary <bha100710 at gmail.com> wrote:

> Strangely
> 
>     root.bind('<Alt-KeyPress-1>', lambda event: nb.select(list[0]))
>     root.bind('<Alt-KeyPress-2>', lambda event: nb.select(list[1]))
>     root.bind('<Alt-KeyPress-3>', lambda event: nb.select(list[2]))
> 
> works
> 
> but
>     root.bind('<Alt-1>', lambda event: nb.select(list[0]))
>     root.bind('<Alt-2>', lambda event: nb.select(list[1]))
>     root.bind('<Alt-3>', lambda event: nb.select(list[2]))
> 
> doesnt whereas
> 
>     root.bind('<Alt-5>', lambda event: nb.select(list[0]))
> 

I think that's probably a window manager issue. Here (debian wheezy with
IceWM) none of the above works, because IceWm uses them to switch between
workspaces. Even when I comment the respective key bindings out in the
IceWm configuration, root.bind('<Alt-1>'..) etc. don't seem to work for
some reason, however root.bind('<Alt-KeyPress-1>'...) and friends seem to
work then.

If you want to add keyboard navigation for the notebook tabs, the best bet
is probably to call enable_traversal() on the Notebook widget which
enables some smart auto-magic, see:
http://www.tcl.tk/man/tcl8.6/TkCmd/ttk_notebook.htm#M35 .
With enable_traversal all you need to do is:

nb = ttk.Notebook()
nb.enable_traversal()
nb.pack(expand=YES, fill=BOTH )
f1=Frame(width=200, height=200, bg="red")
f2=Frame(width=200, height=200, bg="blue")
f3=Frame(width=200, height=200, bg="green")
nb.add(f1, text="red", padding=3, underline=0)
nb.add(f2, text="blue", padding=3, underline=0)
nb.add(f3, text="green", padding=3, underline=0)

Then you can switch the tabs with Alt-r (and so on) or
Control-(Shift)-Tab or the Left and Right arrow keys.

BTW, it is certainly a very bad idea to use the built in name "list" for
a variable, since this will change Python's behavior, as you can see in
this little python session:

$ python3
Python 3.2.3 (default, Feb 20 2013, 17:02:41) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list
<class 'list'>
>>> t = (1, 2, 3)
>>> list(t)
[1, 2, 3]
>>> list=[1, 2]
>>> list
[1, 2]
>>> list(t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
>>> 

Best regards

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Prepare for tomorrow -- get ready.
		-- Edith Keeler, "The City On the Edge of Forever",
		   stardate unknown


More information about the Tkinter-discuss mailing list