2016-04-07 16 views
0

私は簡単に切り替えることができるフレームを持つ2つの機能を使いたいと思っています。私は "行く"フレームを削除することができますが、後で別のフレームを表示する方法がわかりません。 parent機能付きのPythonフレーム間の切り替え

from tkinter import * 

class Trip: 
    def __init__(self, parent): 
     self.go = Frame(parent, width=500, height=450) 
     self.go.grid(row=0, column=0) 
     self.go.grid_propagate(0) # to reserve space required for frame 
     menuButton = Button(self.go, text="Continue", command=self.menuScreen) 
     menuButton.grid(row=1, column=0) 


    def menuScreen(self): 
     self.go.grid_remove() 
     self.menu = Frame(parent, width=500, height=450, bg='orchid') 
     self.menu.grid(row=0, column=0) 
     self.menu.grid_propagate(0) # to reserve space required for frame 

     self.addMore = Button(self.menuScreen, text="Return", command=self.__init__) 
     self.addmore.grid(row=1, column=0) 


if __name__ == "__main__": 
    root = Tk() 
    root.title("Traveller Details") 
    play = Trip(root) 
    root.geometry("500x450+0+0") 
    root.mainloop() 

答えて

0

スコープconstructor初期化に限定しているようです。 menuScreen機能でparentに再度電話しています。

たとえば、parentというクラス内でローカル変数を定義し、コンストラクタに引数としてparent1を渡してみてください。そうすれば、クラス全体でparent属性が表示されます。

私は上記の方法で次の画面に進むことができましたが、ここでは紹介していないコードの他の部分に依存していると思われるその他の問題が発生しました。

ここにコードが変更されています。

from tkinter import * 

class Trip: 
    def __init__(self, parent1): 
     self.parent = parent1 
     self.go = Frame(self.parent, width=500, height=450) 
     self.go.grid(row=0, column=0) 
     self.go.grid_propagate(0) # to reserve space required for frame 
     menuButton = Button(self.go, text="Continue", command=self.menuScreen) 
     menuButton.grid(row=1, column=0) 


    def menuScreen(self): 
     self.go.grid_remove() 
     self.menu = Frame(self.parent, width=500, height=450, bg='orchid') 
     self.menu.grid(row=0, column=0) 
     self.menu.grid_propagate(0) # to reserve space required for frame 

     self.addMore = Button(self.menuScreen, text="Return", command=self.__init__) 
     self.addmore.grid(row=1, column=0) 


if __name__ == "__main__": 
    root = Tk() 
    root.title("Traveller Details") 
    play = Trip(root) 
    root.geometry("500x450+0+0") 
    root.mainloop() 

出力:

enter image description here

+0

これは、2番目のフレームから最初のフレームに戻ることはできません。 –

0

あなたは__init__最初に2つのフレームを作成する必要があります。最初のフレームでtkウィンドウを表示します。

各フレームのクリック可能なボタンを使用して、2つのフレーム間を行き来することができます。

""" 
demonstrate switching back and forth between tkinter frames 
""" 

import tkinter as tk 

class Trip: 
    """ 
    A class to demonstrate switching back and forth between tkinter frames 
    """ 

    def __init__(self, parent): 
     self.parent = parent 
     self.parent.title("Traveller Details") 
     self.parent.geometry("500x450+0+0")   

     self.go_frame = tk.Frame(self.parent, width=500, height=450, bg='light blue') 
     self.goto_menu_frame_button = tk.Button(self.go_frame, text="Continue", command=self.menu_screen) 

     self.menu_frame = tk.Frame(self.parent, width=500, height=450, bg='light steel blue') 
     self.goto_go_frame_button = tk.Button(self.menu_frame, text="Return", command=self.go_screen) 

     self.current_frame = None 
     self.go_screen() 

    def go_screen(self): 
     """ 
     The first screen to be visible - has a clickable button to switch 
     to the other screen 
     """ 
     self.remove_current_frame() 
     self.current_frame = self.go_frame 
     self.go_frame.grid(row=0, column=0) 
     self.go_frame.grid_propagate(0) # to reserve space required for frame 
     self.goto_menu_frame_button.grid(row=1, column=0) 

    def menu_screen(self): 
     """ 
     The second screen - has a clickable button to switch back to the first screen 
     """ 
     self.remove_current_frame() 
     self.current_frame = self.menu_frame 
     self.menu_frame.grid(row=0, column=0) 
     self.menu_frame.grid_propagate(0) # to reserve space required for frame 
     self.goto_go_frame_button.grid(row=0, column=0) 

    def remove_current_frame(self): 
     """ 
     removes the current frame from the grid if it exists 
     """ 
     if self.current_frame is not None: 
      self.current_frame.grid_remove() 

    def start(self): 
     """ 
     launches the GUI 
     """ 
     self.parent.mainloop() 


if __name__ == "__main__": 
    trip = Trip(tk.Tk()) 
    trip.start() 
関連する問題