How can I edit the system menu in the Tkinter window Python? - python

I want to edit the system menu in the Tkinter window to show custom options. When I say system menu, I mean this, the menu that can be opened by right-clicking the title bar of a window, not this, the dropdown menu in a window.
I want to be able to add as many options as I like, not just the default ones(shown below).
May I know how I can do that? Thanks.

Related

normal button that acts like menubutton python tkinter

I am trying to write code that you can turn a normal Tkinter button into a dropdown menu but the dropdown menu button is in the main window, not in the menu bar. kind of like notepad++ how the menu bar is on the window, but I am making a dropdown button like that but that shows in the window. Can someone help? I don't really know how to explain this, but I am a beginner. I am using python and Tkinter on pycharm on a mac.
here is a video I made to explain more:
https://www.hippovideo.io/video/play/nuvNU03a6Hg7_oQC39uy1wgopPijjQQGCLH18VC0KiA
You can create popup-menus with menu.tk_popup(x, y).
So just add master.bind('<Button-3>', lambda e:menu.tk_popup(x, y).
x and y are values which you choose. If you want to place the menu at the mouse-pointer, you can use master.geometry(), but to be honset, I have no idea how to use master.geometry() exactly. But there are other Solutions too, e.g. tkinter.Spinbox().

Balloon box with optionmenu in tkinter (python)

I am creating a GUI that contains a drop-down box (called an optionmenu in tkinter). I have it populated with entries.
This is what I want to do but can't figure out the proper commands. If a user hovers their mouse over a specific entry in the option menu, I want a hoverbox/balloon box/tooltip (whatever you want to call it) to appear and show a description of the option they are hovering over.
I assume I have to use some sort of event but I am not sure. Does anyone know how?

Getting an existing menu for tkinter Toplevel

I know how to create and set menus for tkinter Toplevel windows, but I'm struggling to find any information on how to get a window's menu bar. What I'd like to do is dynamically add options to the menu, so I need to do something like:
menubar = self.getMenu()
menubar.add_cascade(...)
Where self is a Toplevel window. Thanks!
You want to use the cget method, which can be used to get any of the configured options:
menu = self.cget("menu")

'hover over' popup with Tkinter

I have implemented an informational popup in a python app using a Tkinter Menu widget. I have a Text widget on a canvas in the root window. I created a Menu widget that has root as its parent. When I detect a mouse hover over the text widget I post the popup menu with menuWidget.post(). When I get a leave event from the text widget my intention was to have the popup disappear by calling menuWidget.unpost(), only the popup menu does not disappear until I click elsewhere outside the text widget.
First, is this a sane method for implementing an informational popup? And can anyone tell me why the popup menu won't disappear?
This is not the right way to do an informational popup. On the Mac and on windows machines menus are native controls. Because of this the unpost command doesn't work because tk cedes control to the system event loop in order to get platform-specific behavior.
What you want is to use instead is a toplevel window with the overrideredirect flag set. This lets you display a borderless window anywhere you want. The upside to this is that you aren't limited to simple text -- you can put anything you want in that toplevel -- another text widget, a canvas, buttons, etc.

How to change the OSX menubar in wxPython without any opened window?

I am writing a wxPython application that remains open after closing all of its windows - so you can still drag & drop new files onto the OSX dock icon (I do this with myApp.SetExitOnFrameDelete(False)).
Unfortunately if I close all the windows, the OSX menubar will only contain a "Help" menu. I would like to add at least a File/Open menu item, or just keep the menubar of the main window. Is this somehow possible in wxPython?
In fact, I would be happy with a non-wxPython hack as well (for example, setting the menu in pyobjc, but running the rest of the GUI in wxPython). wxPython development in OSX is such a hack anyway ;)
UPDATE: I managed to solve this problem using the tip from Lyndsey Ferguson. Here's what I have done:
On startup I create a window which I show and hide immediately. I set its position to (-10000,-10000) so that it does not flicker on the screen (aargh, what a dirty hack!)
I create an empty EVT_CLOSE event handler in that window so that it cannot be closed.
It seems that destroying a window resets the OSX menu, but hiding does not... So when the last window is closed, I need to show and hide this window again (hiding is necessary so that the user cannot switch to this window using the Window menu or Cmd-`)
Yeah, this is really ugly... I will be very grateful if someone comes up with a prettier solution.
UPDATE 2: Actually it can be solved in a much easier way: if we do not close the last window, only hide it. And ensure that it does not respond to menu events anymore.
Nowadays you can use wx.MenuBar.MacSetCommonMenuBar() to set the menu bar (which you have to create) that should be used when no windows are open.
If you just want a default macOS menu bar to be used (with the application and Window menus already there), this appears to be the minimal code:
menubar = wx.MenuBar()
wx.MenuBar.MacSetCommonMenuBar(menubar)
This will let your app respond to Command+Q out-of-the-box, too.
The wx.MenuItem IDs wx.ID_ABOUT and wx.ID_EXIT are special as menu items with those IDs are moved to the macOS Application menu. The docs actually refer to the application menu as the "Apple" menu (e.g. the menu described in the wx.MenuBar.OSXGetAppleMenu() function's docs is the application menu), possibly for historical reasons.
Can you create a hidden window that is offscreen somewhere? It is a hack, but I remember having to do a lot of hacks to make my wxPython-based application work correctly on Mac OS X.
Note:You'll have to disable the close button and set up that hidden window so that it doesn't show up in the Window menu.
Aside:Have you considered factoring out your GUI portion of your Python application and using PyObjC on Mac OS X? You'll get more native behaviours...

Categories