2017-09-06 5 views
0

Bryan Oakleyの答え「tkinterの2つのフレームを切り替える」の質問から、2ページ目のボタンの動作を変更します。関数を使用してtkinterのフレームを切り替える

ページ1では、command=lambda: controller.show_frame(“StartPage”)と表示されています。

2ページで、何かを追加して元に戻したいが、動作しない。

私のコールバックが呼び出されないのはなぜですか?

import tkinter as tk    # python 3 
from tkinter import font as tkfont # python 3 

class SampleApp(tk.Tk): 

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 

     self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic") 

     # the container is where we'll stack a bunch of frames 
     # on top of each other, then the one we want visible 
     # will be raised above the others 
     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, PageTwo): 
      page_name = F.__name__ 
      frame = F(parent=container, controller=self) 
      self.frames[page_name] = frame 

      # put all of the pages in the same location; 
      # the one on the top of the stacking order 
      # will be the one that is visible. 
      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame("StartPage") 

    def show_frame(self, page_name): 
     '''Show a frame for the given page name''' 
     frame = self.frames[page_name] 
     frame.tkraise() 


class StartPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 
     label = tk.Label(self, text="This is the start page", font=controller.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")) 
     button2 = tk.Button(self, text="Go to Page Two", 
          command=lambda: controller.show_frame("PageTwo")) 
     button1.pack() 
     button2.pack() 


class PageOne(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 
     label = tk.Label(self, text="This is page 1", font=controller.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() 


class PageTwo(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 
     label = tk.Label(self, text="This is page 2", font=controller.title_font) 
     label.pack(side="top", fill="x", pady=10) 
     button = tk.Button(self, text="Go to the start page", 
          command=self.go_to()) 
     button.pack() 
    def go_to(self): 
     # Add something to do    
     self.controller.show_frame("StartPage") 


if __name__ == "__main__": 
    app = SampleApp() 
    app.mainloop() 

答えて

0

問題はButtoncommandキーワード引数は、関数を取ることです。 tk.Button(self, ..., command=self.go_to())でボタンを作成するので、コマンドはself.go_to()であり、これはNoneと評価されます。

なぜそうですか?次のように go_toが定義されています

def go_to(self): 
    # Add something to do 
    self.controller.show_frame("StartPage") 

Pythonはcommand=self.go_to()渡って来るときので、それはgo_toを呼び出し、commandオプションに返された値を渡します。 この値はNonereturnステートメントがないことを意味します)、コマンドはNoneであり、ボタンは無効です。

括弧を削除する必要がありますので、tk.Button(self, ..., command=self.go_to)が必要です。

+0

ありがとうございます。それは私の目の前にあり、私はそれを見ることができませんでした。ありがとう – ogeretal

+0

go_toが何かをするためにパラメータを使用して、それがStartPageに戻った場合、どのように実装しますか? – ogeretal

+0

@ogeretal引数なしの関数をラップするか、 'command = lambda:self.go_to(x、y)'のようなラムダを使用します。そのラムダは「引数をとらず、xとyでfを呼び出す関数」を意味するので、期待どおりの関数です。 –

関連する問題