Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

python - Creating a menuBar in a frame?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...