-2

我想在这段代码中放一个菜单(我在这个网站上找到的);但我不知道如何,也没有找到答案。

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

EDIT2:我已经解决了,谢谢。

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()
4

1 回答 1

0

添加类似的东西

    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.__init__.


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()
于 2013-03-04T21:46:27.853 回答