在框架中创建菜单栏? [英] 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()
我发布的上述代码工作正常,但菜单显示在所有窗口中,但我希望菜单仅显示在一个窗口中....我使用了controller.configure,因此菜单显示在所有窗口中,但如果我使用 self.configure 我收到一个错误...有人可以帮我吗
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
推荐答案
不能将菜单附加到框架.唯一带有菜单选项的小部件是根窗口和顶层窗口.
You cannot attach a menu to a frame. The only widget with a menu option is the root window and Toplevel windows.
如果你想把菜单放在一个框架中,你需要创建一个框架,添加一个或多个Menubutton 小部件,并为每个小部件附加一个菜单.
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.
这篇关于在框架中创建菜单栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!