Skip to content
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

Open
5 tasks
casmithac opened this issue Jan 31, 2024 · 10 comments
Open
5 tasks
Labels
enhancement New feature or request Port - TK PySimpleGUI

Comments

@casmithac
Copy link

casmithac commented Jan 31, 2024

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]

  • [x ] Searched main docs for your problem www.PySimpleGUI.org
  • [x ] Looked for Demo Programs that are similar to your goal. It is recommend you use the Demo Browser! Demos.PySimpleGUI.org
  • [x ] None of your GUI code was generated by an AI algorithm like GPT
  • If not tkinter - looked for Demo Programs for specific port
  • For non tkinter - Looked at readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • Run your program outside of your debugger (from a command line)
  • Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.org
  • [x ] Have upgraded to the latest release of PySimpleGUI on PyPI (lastest official version)
  • Tried using the PySimpleGUI.py file on GitHub. Your problem may have already been fixed but not released

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

import PySimpleGUI as sg

menu_def = [['File'],['Wizards',['Calibrate Steps','Cut Keyhole']],
            ['Probe'],['Motion'],['Macros'],['Exit']
            ]
layout = [
        [
            sg.Menu(menu_def)
        ],
        [
            sg.T("This shows how pressing Exit does not create an Event")
        ]
        ]
window = sg.Window("Test", layout)
while True:
    event, values = window.read() #  in msec

    if event == "Exit" or event == sg.WIN_CLOSED:
        break

Screenshot, Sketch, or Drawing

MenusBug


Watcha Makin?

CNC/Laser Control Program

@pysimpleissue pysimpleissue bot closed this as completed Jan 31, 2024
@pysimpleissue pysimpleissue bot added Fill issue form or you will be REJECTED You MUST use the supplied template to submit a request. PySimpleGUI Issues Bot Has Detected an Error and removed Fill issue form or you will be REJECTED You MUST use the supplied template to submit a request. PySimpleGUI Issues Bot Has Detected an Error labels Jan 31, 2024
@pysimpleissue pysimpleissue bot reopened this Feb 1, 2024
Repository owner deleted a comment from pysimpleissue bot Feb 1, 2024
Repository owner deleted a comment from pysimpleissue bot Feb 1, 2024
Repository owner deleted a comment from pysimpleissue bot Feb 1, 2024
@jason990420
Copy link
Collaborator

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()

@jason990420 jason990420 added the Port - TK PySimpleGUI label Feb 1, 2024
@casmithac
Copy link
Author

casmithac commented Feb 1, 2024 via email

@casmithac
Copy link
Author

Thanks again!
I did not close this . I'm leaving it to the developers to close in case this will be added
as a feature in future releases of PySimpleGUI and this will be it's place holder.

@PySimpleGUI PySimpleGUI changed the title [ Question] Are There Top Level Menu Events? [ Enhancement ] Add event generation for top level menu items that have no submenus Feb 1, 2024
@PySimpleGUI PySimpleGUI added the enhancement New feature or request label Feb 1, 2024
@PySimpleGUI
Copy link
Owner

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.

@casmithac
Copy link
Author

You may already have this in mind when this enhancement is implmented:
As you hover over a top level menu item that is now clickable, highlight it by changing the background.

And, can you edit the workaround code to reflect this?
Thanks,
Craig

@PySimpleGUI
Copy link
Owner

I see highlights when running Jason's code. The existing Menu element also highlights.

@casmithac
Copy link
Author

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.

@PySimpleGUI
Copy link
Owner

The operating system defines the colors used on Menubars.

The MenubarCustom element was created so that the Menubar itself can have the colors change. The highlight color is not broken out separately at the moment. Highlights are the inverse of the bar/text colors. There are 3 Demo Programs that use MenubarCustom that I count.

@casmithac
Copy link
Author

Thanks, that worked by using use_custom_titlebar=True when opening the window.
Screenshot 2024-02-08 084612
In the screenshot, if I left click on the top level menu and attempt to move the form, it doesn't move. If I left ckick on the blue title bar just below it, I can move the form. Is there a window option to allow this?
Thanks,
Craig

@PySimpleGUI
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Port - TK PySimpleGUI
Projects
None yet
Development

No branches or pull requests

3 participants