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")
Related
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.
I'm currently working with Tkinter and Python 2.7 on Linux and I was wondering if there was a way to remove the TK() window border frame and title bar without using overrideredirect(1).
I have my own close button and overrideredirect(1) presents me with a few issues that I can't accept:
GUI always on top
can't iconify then deiconify properly
no keyboard input so can't type into fields (see python tkinter overrideredirect; cannot receive keystrokes (Linux))
I can't use attributes("-fullscreen", True) as the titlebar and borders remain.
The window decoration is all handled by the window manager so what you are trying to do is find a way to tell the window manager to decorate your window differently from a standard application window. Tk provides overrideredirect to have the window manager completely ignore this window but we can also use Extended Window Manager Hints to declare the intended use of this toplevel window to the window manager. This is done for instance for tooltip and splashscreen windows to allow the manager to provide minimal decoration and possibly special animations.
In your case, adding a 'splash' hint should do what you want
root = tk.Tk()
root.wm_attributes('-type', 'splash')
You will need Tk 8.5 or above for this.
You must give your root window name before your command.
Like this:
from tkinter import *
root=Tk()
root.wm_attributes('-fullscreen','true')
root.mainloop()
I have multiple buttons in my tkinter 8.5 GUI (on Windows 7). I want whatever button is focused on (tabbed over) to be selected when the user hits Enter. I know I have to bind '<Return>', but I need the rest of the gaps filled in.
Thanks in advance!
Assuming you want this to be universal to all applications in the root window you could do something similar to this.
def clickButton():
widget = root.focus_get()
if widget != root:
widget.invoke()
root = Tkinter.Tk()
root.bind("<Return>", clickButton)
root.mainloop()
That will run any command associated with the currently tabbed selection. If you want to limit it to certain buttons you can do type-checking inside of the method. Widget will be whatever widget is currently in focus via the tabbed selection. Also beware of a user hitting enter on certain widgets that may not support the invoke method.
What is the best way to change the state of a tkinter menu or its children after the mainloop?
Thanks
You configure it the way you configure all widgets, with the configure method:
widget.configure(state="disabled")
If you want to disable one of the items on the menu, use entryconfig:
widget.entryconfig(0, state="disabled")
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.