python中的菜单tk

python中的菜单tk,python,menu,tkinter,Python,Menu,Tkinter,我想把这个代码(我在这个网站上找到)菜单;但我不知道怎么做, 没有找到答案 编辑:主要问题是,每当我尝试放置出现在其他窗口(如tk#2)中的菜单时 我已经解决了,谢谢 import tkinter as tk TITLE_FONT = ("Helvetica", 18, "bold") class SampleApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs)

我想把这个代码(我在这个网站上找到)菜单;但我不知道怎么做, 没有找到答案

编辑:主要问题是,每当我尝试放置出现在其他窗口(如tk#2)中的菜单时

我已经解决了,谢谢

import tkinter as tk
TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(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 = {}
    for F in (StartPage, PageOne):
        frame = F(container, self)
        self.frames[F] = frame
        frame.grid(row=0, column=0, sticky="nsew")
    self.show_frame(StartPage)
def show_frame(self, c):
    frame = self.frames[c]
    frame.tkraise()
class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) 
        label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
         button1 = tk.Button(self, text="Go to Page One", 
                        command=lambda: controller.show_frame(PageOne))
         button1.pack()
class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) 
        label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page", 
                           command=lambda: controller.show_frame(StartPage))
        button.pack()
if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
添加如下内容

    menubar = tk.Menu(self)
    file_menu = tk.Menu(menubar)
    file_menu.add_command(label='Quit', command=sys.exit)
    menubar.add_cascade(label='File', menu=file_menu)
    self.config(menu=menubar)
SampleApp.\uuuu init\uuuu



如果您发布特定的代码并提及您遇到的问题,而不是要求别人为您编写代码,您可能会更幸运。问题是,每次我放置菜单时,该菜单都位于其他位置windows@EmmettJ.Butler我不能写具体的代码,因为我不知道是什么造成了这个问题
import sys
import tkinter as tk
TITLE_FONT = ("Helvetica", 18, "bold")


class SampleApp(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)

        menubar = tk.Menu(self)
        file_menu = tk.Menu(menubar)
        file_menu.add_command(label='Quit', command=sys.exit)
        menubar.add_cascade(label='File', menu=file_menu)
        self.config(menu=menubar)

        self.frames = {}
        for F in (StartPage, PageOne,
                  # PageTwo
                  ):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, c):
        frame = self.frames[c]
        frame.tkraise()


class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button1 = tk.Button(self, text="Go to Page One",
                                command=lambda: controller.show_frame(PageOne))
        button1.pack()


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame(StartPage))
        button.pack()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()