问题
I'm trying to edit Menu window. How can i edit it?
This is for Windows.
from tkinter import *
Window = Tk()
MB = Menu(Window)
Window.config(menu=MB)
Menubar = Menu(MB)
MB.add_cascade(label="File", menu=Menubar)
Menubar.add_command(label="New File")
#Btn1 = Button(Menubar, width=20, text="Button").pack() #I failed.
Window.mainloop()
I tried Btn1 = Button(Menubar, width=20, text="Button").pack()
But this one couldn't work.
回答1:
usually you add items to menu like this - but what are looking for?
window = Tk()
mb = Menu(window)
menu_bar = Menu(mb, tearoff=0)
menu_bar.add_command(label="New")
menu_bar.add_command(label="Open")
menu_bar.add_command(label="Save")
menu_bar.add_command(label="Save as...")
menu_bar.add_command(label="Close")
menu_bar.add_separator()
menu_bar.add_command(label="Exit", command=window.quit)
mb.add_cascade(label="File", menu=menu_bar)
top.config(menu=mb)
回答2:
You cannot add arbitrary widgets to a menu. To add items to a menu you must use one of the following functions available on the Menu object:
add
add_command
add_cascade
add_checkbutton
add_radiobutton
add_separator
To add a textual label you can use add_command
and set the command
attribute to None
.
All of these are documented with the Menu widget definition itself, and on just about any site that includes widget documentation.
回答3:
If you're looking for a control of Menu Bar with Buttons you can do it. All of the code will be in the same file.py .Libraries in Python3 (For Python 2 change to import Tkinter as tk). Later you can redirect to each window with each menu with buttons.
import tkinter as tk
from tkinter import ttk
Global list of titles for
nombre_ventanas={'PageOne'}
One menubar, you need to add similar for each menu that you want to display. menubar_global, menubar1, menubar2, ...
def menubar_global(self, root, controller):
menubar_global = tk.Menu(root)
page_1 = tk.Menu(menubar_global, tearoff=0)
# With Submenu for page_1
page_1_1=tk.Menu(page_1 , tearoff=0)
page_1_2=tk.Menu(page_1 , tearoff=0)
Main
if __name__ == "__main__":
app = ControlWindow()
app.mainloop()
Class for windows control
class ControlWindow(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
self._nombre_ventanas = nombre_ventanas
for F in self._nombre_ventanas:
F = eval(F)
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
# First window
self.show_frame(Login)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
# Title for each page
self.title(titulo_ventanas[cont._name_for_menu_bar])
# Here you can add if-sentece for each menubar that you want
if cont._name_for_menu_bar != "PageOne":
# Menu bar
print("show_frame", cont)
menubar = frame.menubar(self)
self.configure(menu=menubar)
else:
menubar = 0
self.configure(menu=menubar)
class Ventana_proveedores(tk.Frame):
_name_for_menu_bar = "Ventana_proveedores"
_config = configuracion
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
tk.Button(self, text='Regresar', font=FONT_LABELFRAME,
command=lambda: controller.show_frame(Ventana_gerente)).grid(row=0, column=0)
def menubar(self, root):
return menubar_global(self, root, self.controller)
Later Each Page need to have this configuration
class PageOne(tk.Frame):
_name_for_menu_bar = "PageOne"
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
def menubar(self, root):
return menubar_global(self, root, self.controller)
来源:https://stackoverflow.com/questions/57465257/python-tkinter-edit-menu-window