Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

the above code which i posted works fine but the menu displays in all the windows but i wanted the menu to displayed in only one window....i used controller.configure so the menu is displayed in all windows but if i use self.configure i am getting an error...can some one please help me

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
377 views
Welcome To Ask or Share your Answers For Others

1 Answer

You cannot attach a menu to a frame. The only widget with a menu option is the root window and Toplevel windows.

If you want to put a menu inside a frame, you will need to create a frame, add one or more Menubutton widgets, and attach a menu to each of those.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...