Is it possible? By looking at the options I'm stumped. Searching on the web hasn't lead me anywhere. Can I create a submenu in the menubar. I'm referring to doing something similar to Idle Shell when I click on File and go down to Recent Files and it pulls up a separate file showing the recent files I've opened.
If it's not possible what do I have to use to get it to work?
Creating Python Submenus A submenu is a nested menu that shows up while you move the cursor over a given menu option. To add a submenu to an application, you need to call . addMenu() on a container menu object. Say you need to add a submenu in your sample application's Edit menu.
A popup Menu can be created by initializing tk_popup(x_root,y_root, False) which ensures that the menu is visible on the screen. Now, we will add an event which can be triggered through the Mouse Button (Right Click). The grab_release() method sets the mouse button release to unset the popup menu.
You do it exactly the way you add a menu to the menubar, with add_cascade
. Here's an example:
# Try to import Python 2 name
try:
import Tkinter as tk
# Fall back to Python 3 if import fails
except ImportError:
import tkinter as tk
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
menubar = tk.Menu(self)
fileMenu = tk.Menu(self)
recentMenu = tk.Menu(self)
menubar.add_cascade(label="File", menu=fileMenu)
fileMenu.add_cascade(label="Open Recent", menu=recentMenu)
for name in ("file1.txt", "file2.txt", "file3.txt"):
recentMenu.add_command(label=name)
root.configure(menu=menubar)
root.geometry("200x200")
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With