pyTermTk
pyTermTk copied to clipboard
[Question] React to sub-menubar event/signal
Hi there, I'm sorry to bother, but I just can't figure out how to handle event/signals from submenu's. I tried something like
def menu_pressed(button):
ttk.TTkLog.debug(f"menu_pressed: {button.text}")
menu = frameTop.menubarTop().addMenu("Menu Button")
submenu = menu.addMenu("Category 1")
submenu.addMenu("New").menuButtonClicked.connect(menu_pressed)
other_submenu = menu.addMenu("Category 2")
other_submenu.addMenu("New").menuButtonClicked.connect(menu_pressed)
But now I can not distinguish between them since the emited button.text
is in both cases "New". I also cannot use a different callback function since the menu is suppossed to be generated on runtime.
I really like this library and really appreciate any help.
Thanks a lot!
I admit that the menubar widget require a major rework, in the meantime;
Idea 1
The addMenu function return the "Button" object you can use in your "menu_pressed" function in order to identify it: https://github.com/ceccopierangiolieugenio/pyTermTk/blob/f926d7d71c09cf4bc629c013d54c3d0a7eef536c/TermTk/TTkWidgets/menubar.py#L92-L96
Idea 2
You can use a lambda function i.e.:
import sys, os
import TermTk as ttk
def menu_pressed(button, val, desc):
ttk.TTkLog.debug(f"menu_pressed: {button.text} {val} {desc} {button}")
root = ttk.TTk()
win = ttk.TTkWindow(parent=root,pos=(1,1), size=(105,40), border=True, layout=ttk.TTkGridLayout())
menu = win.menubarTop().addMenu("&File")
submenu = menu.addMenu("Category 1")
submenu.addMenu("New").menuButtonClicked.connect(lambda b: menu_pressed(b,10,"C.1"))
other_submenu = menu.addMenu("Category 2")
other_submenu.addMenu("New").menuButtonClicked.connect(lambda b: menu_pressed(b,20,"C.2"))
ttk.TTkLogViewer(parent=win)
root.mainloop()
you can run this code in the sandbox:
Idea 3
I am pretty sure something else is possible
Let me know if this can be useful, As I said the menubar require a major rework so any suggestion is well accepted
Idea 1
This didn't worked for me. I could identify buttons of the first level by referncing to button._parent._parent._parent._title
. But this didn't work for buttons of level 2 and up.
Idea 2
This is what I ended up implementing. Thank you.
Idea 3
It is hard to tell how, but either a "clickable-parent" attribute which delivers the next parent that can be clickable. Or an "parent-tree" to parse through including the class name and _title
could do.
As stated I was unable to produce a proper selector for myself by going through the exiting parents by doing button._parent._parent.....