New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ Enhancement ] Add event generation for top level menu items that have no submenus #6661
Comments
Workaround here, but tkinter code required. import PySimpleGUI as sg
menu_def = [['File'], ['Wizards',['Calibrate Steps','Cut Keyhole']], ['Probe'], ['Motion'], ['Macros'], ['Exit']]
layout = [
[sg.Menu(menu_def, key='Menu')],
[sg.T("This shows how pressing Exit does not create an Event"),]
]
window = sg.Window("Test", layout, finalize=True)
# Workaround to add event to main item of the Menu element
menu = window['Menu']
for item in menu_def:
index = item[0]
if index != 'Wizards':
menu.widget.entryconfigure(index, menu='', command=lambda item=index:menu._MenuItemChosenCallback(item))
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
print(event, values)
window.close() |
Hi Jason,That worked! Thank you for your quick solution!
Craig
On Wednesday, January 31, 2024 at 10:28:12 PM EST, Jason Yang ***@***.***> wrote:
Workaround here, but tkinter code required.
import PySimpleGUI as sg
menu_def = [['File'], ['Wizards',['Calibrate Steps','Cut Keyhole']], ['Probe'], ['Motion'], ['Macros'], ['Exit']]
layout = [
[sg.Menu(menu_def, key='Menu')],
[sg.T("This shows how pressing Exit does not create an Event"),]
]
window = sg.Window("Test", layout, finalize=True)
# Workaround to add event to main item of the Menu element
menu = window['Menu']
for item in menu_def:
index = item[0]
if index != 'Wizards':
menu.widget.entryconfigure(index, menu='', command=lambda item=index:menu._MenuItemChosenCallback(item))
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
print(event, values)
window.close()
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Thanks again! |
You're quite welcome! Thank you for the "thanks"! Love it when our users appreciate Jason's contributions. I knew he would quickly come up with a way to get this working as you desired. I've edited the title and added the Enhancement label so we can prioritize adding it as a feature. |
You may already have this in mind when this enhancement is implmented: And, can you edit the workaround code to reflect this? |
I see highlights when running Jason's code. The existing Menu element also highlights. |
The top level, in mine, has a little bit of a highlight as you hover over. When you pull down the Wizard menu and hover over the two items there, they are highlighted with a darker color, black I believe. |
The operating system defines the colors used on Menubars. The |
You need a MenubarCustom if using a Custom Titlebar. You can use a MenubarCustom without a Custom Titlebar. You can't mix a normal Menu with a custom titlebar. |
Type of Issue (Enhancement, Error, Bug, Question)
Question
Operating System
Windows 23H2 (OS Build 22631.3085)
PySimpleGUI Port = tkinter
Versions
Version information can be obtained by calling
sg.main_get_debug_data()
Or you can print each version shown in ()
Python Interpeter: C:\Python\Python386\python.exe
Python version: 3.8.6
Platform: Windows
Platform version: ('10', '10.0.22621', 'SP0', 'Multiprocessor Free')
Port: PySimpleGUI
tkinter version: 8.6.9
PySimpleGUI version: 4.61.0.206
PySimpleGUI filename: C:\Python\Python386\lib\site-packages\PySimpleGUI\PySimpleGUI.py"
Your Experience In Months or Years (optional)
Years Python programming experience = about 4 years
Years Programming experience overall = 53 years, started in high school
Have used another Python GUI Framework? (tkinter, Qt, etc) (yes/no is fine) = no
Anything else you think would be helpful?
Troubleshooting
These items may solve your problem. Please check those you've done by changing - [ ] to - [X]
Detailed Description
I'd like the top level menu item to create an event. This means a menu item with
no sub menu. The sub menus do cause an event, top levels do not. I'd like a menu
with an Exit or others to trigger an event.
Code To Duplicate
Screenshot, Sketch, or Drawing
Watcha Makin?
CNC/Laser Control Program
The text was updated successfully, but these errors were encountered: