在框架中创建menuBar?

时间:2014-07-21 06:55:00

标签: python tkinter

import Tkinter as tk
from Tkinter import Label, StringVar, Entry, Button, Menu, Frame, Tk, Canvas
import subprocess
from Tkconstants import LEFT, CENTER,W, SUNKEN , X, Y

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)
            print "Hellooo Stack"
            self.frames = {}
            for F in (MainWindow,Master):
                frame = F(container, self)
                self.frames[F] = frame

                frame.grid(row=0, column=0, sticky="nsew")

            self.show_frame(MainWindow)

        def show_frame(self, c):
            '''Show a frame for the given class'''
            frame = self.frames[c]
            frame.tkraise()
class MainWindow(tk.Frame):
        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)

            button = tk.Button(self, text="Connections",compound=LEFT)
            button.pack(pady=50)
            button1 = tk.Button(self, text="   Locker            ", compound=LEFT,
                                    command=lambda: controller.show_frame(Master))
            button1.pack(pady=50)

class Master(tk.Frame): 
       def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            menubar = Menu(self)
            filemenu = Menu(menubar,tearoff=0)
            filemenu.add_command(label="New",)
            filemenu.add_command(label="Open",)
            filemenu.add_command(label="Save As..")
            filemenu.add_command(label="Colour")
            filemenu.add_command(label="Close")
            menubar.add_cascade(label="File",menu=filemenu)
            controller.configure(menu = menubar)
if __name__ == "__main__":

        app = SampleApp()
        app.title("APPS")

        app.mainloop()

我发布的上述代码工作正常但菜单显示在所有窗口但我希望菜单只显示在一个窗口....我使用controller.configure所以菜单显示在所有窗口,但如果我使用self.configure我收到错误...有人可以帮助我

1 个答案:

答案 0 :(得分:2)

您无法将菜单附加到框架。唯一带有菜单选项的小部件是根窗口和Toplevel窗口。

如果要在框架中放置菜单,则需要创建一个框架,添加一个或多个Menubutton窗口小部件,并为每个窗口小部件附加一个菜单。