2013-03-04 26 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