Hi Michael,
here is a snipped of a code and what I've managed up to now
Mouse left click opens a standard menu with columns
Mouse right click opens the same menu with columns and my first try to create a
Left/Right keyboard bindings to navigate in the columns.
My attempt fails on the first row and the last column...
best
Vasilis
import tkinter as tk
# ------------------------------------------------------------------------------
# Right key menu handler
# ------------------------------------------------------------------------------
def menuRight(event):
menu = event.widget
index = menu.index(tk.ACTIVE)
last = menu.index(tk.LAST)
ypos = menu.yposition(index)
yprev = ypos
for i in range(index+1, last):
if yprev < ypos <= menu.yposition(i):
# WARNING empty entries appear as command without a label!
# ignore them
if menu.entrycget(i,"label") != "":
menu.activate(i)
return "break"
yprev = menu.yposition(i)
# ------------------------------------------------------------------------------
# Left key menu handler
# ------------------------------------------------------------------------------
def menuLeft(event):
menu = event.widget
index = menu.index(tk.ACTIVE)
ypos = menu.yposition(index)
yprev = ypos
for i in range(index-1, -1, -1):
if menu.yposition(i) <= ypos < yprev:
# WARNING empty entries appear as command without a label!
# ignore them
if menu.entrycget(i,"label") != "":
menu.activate(i)
return "break"
yprev = menu.yposition(i)
#-------------------------------------------------------------------------------
def show_menu(event, with_binding):
global root
menu = tk.Menu(root)
for label in ["one","two","three","four"]:
submenu = tk.Menu(menu)
menu.add_cascade(label=label, menu=submenu)
for i in range(101):
submenu.add_command(label=f"Item #{i}")
if i and i%20==0:
submenu.entryconfigure(i, columnbreak=1)
if with_binding:
submenu.bind("<Right>", menuRight)
submenu.bind("<Left>", menuLeft)
if with_binding:
menu.bind("<Right>", menuRight)
menu.bind("<Left>", menuLeft)
menu.tk_popup(event.x_root, event.y_root)
#-------------------------------------------------------------------------------
if __name__ == "__main__":
root = tk.Tk()
tk.Label(root, text="<1> Left click to show menu without Left/Right
binding").pack()
tk.Label(root, text="<3> Right click to show menu with Left/Right
binding").pack()
tk.Label(root, text="<q> to exit").pack()
root.bind("<1>", lambda e: show_menu(e,False))
root.bind("<3>", lambda e: show_menu(e,True))
root.bind("<Key-q>", lambda e: root.destroy())
root.mainloop()
________________________________
From: Tkinter-discuss
<[email protected]> on behalf of
Michael Lange <[email protected]>
Sent: Thursday 14 September 2023 09:45
To: [email protected] <[email protected]>
Subject: Re: [Tkinter-discuss] Navigation in menu with multiple columns
Hi Vasilis,
On Wed, 13 Sep 2023 15:57:10 +0000
Vasilis Vlachoudis <[email protected]> wrote:
> In my application I am dynamically creating cascading menus, where some
> of them have multiple entries. To imrpove the visualization I break
> them into multiple columns using the "columnbreak=1" attribute in the
> Menu.add_command()
>
> The Up/Down keys move correctly the active/highlighted entry up/down
>
> However the Left key closes always the cascade submenu, while I would
> prefer to highlight the entry on the left column, and when it is on the
> left-most column to close the cascade submenu Is there a way to bind
> the Left/Right keys to move left/right in the columns?
>
> Vasilis
could you please post a simple example that shows this behaviour?
Have a nice day,
Michael
.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.
Peace was the way.
-- Kirk, "The City on the Edge of Forever", stardate unknown
_______________________________________________
Tkinter-discuss mailing list
[email protected]
https://mail.python.org/mailman/listinfo/tkinter-discuss
_______________________________________________
Tkinter-discuss mailing list
[email protected]
https://mail.python.org/mailman/listinfo/tkinter-discuss