2013-03-04 29 views
-2

我想在此代码中放置一个菜单(我在此网站上找到该菜单);但我不知道如何, 并没有找到答案。Python中的菜单tk

编辑:主要的问题是,每当我试图把出现在其他窗口(如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() 
+0

你可能有更多的运气,如果你发布特定的代码,并提及您遇到,而不是请求,有人编写代码对你的问题。 – 2013-03-04 21:37:22

+0

问题是,每次我把一个菜单,该菜单是在其他窗口 – 2013-03-04 21:38:29

+0

@ EmmettJ.Butler,我不能把具体的代码,因为我不知道什么使这个问题 – 2013-03-04 21:40:38

回答

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

thx真的谢谢 – 2013-03-04 21:48:24