Python Tkinter Edit Menu Window

前端 未结 3 1562
太阳男子
太阳男子 2021-01-28 11:49

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)

         


        
3条回答
  •  逝去的感伤
    2021-01-28 12:41

    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)
    

提交回复
热议问题