[英]Switching between frames in python with functions
我希望有兩個功能,在其中可以輕松切換的框架。 我可以刪除“ go”框架,但是不確定如何顯示其他框架。
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()
parent
范圍似乎僅限於constructor
初始化。 您正在menuScreen
函數中再次調用parent
menuScreen
。
嘗試在類類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()
輸出:
首先需要在__init__
創建兩個框架; 然后顯示帶有第一幀的tk窗口。
每個框架上的可單擊按鈕允許您在兩個框架之間來回切換。
"""
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.