Python 如何使tkinter程序转到上一帧?
我正在构建一个小程序,其中代码的不同部分将在不同时间调用一个对象。我想尝试让它调用前一帧,而不是重复实际代码多次。如果您需要示例,以下是我的部分代码:Python 如何使tkinter程序转到上一帧?,python,user-interface,tkinter,Python,User Interface,Tkinter,我正在构建一个小程序,其中代码的不同部分将在不同时间调用一个对象。我想尝试让它调用前一帧,而不是重复实际代码多次。如果您需要示例,以下是我的部分代码: #Console Menu class consoleMenu(tk.Frame): #Initialize def __init__(self, parent, controller): tk.Frame.__init__(self, parent)
#Console Menu
class consoleMenu(tk.Frame):
#Initialize
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#Setups
consoleGuideLabel = ttk.Label(self, text = "Console Guide", font = LARGE_FONT)
consoleItemInfoButton = ttk.Button(self, text = "Console Item Info", command = lambda: controller.show_frame(consoleItemInfo))
consoleVersionButton = ttk.Button(self, text = "Console Version History", command = lambda: popupmsg ("Not supported just yet!"))
consoleMainMenuButton = ttk.Button(self, text = "Main Menu", command = lambda: controller.show_frame(StartPage))
#Placement
consoleGuideLabel.pack(pady = 10, padx = 10)
consoleItemInfoButton.pack()
consoleVersionButton.pack()
consoleMainMenuButton.pack()
这就是被不同帧多次调用的部分
#Air - MCPE, PC, Xbox
class mc_air(tk.Frame):
#Initialize
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#Item Info
text_file = open("minecraft_air.txt", "r")
file = text_file.read()
text_file.close()
#Setups
airLabel = ttk.Label(self, text = "Minecraft - Air - 1", font = LARGE_FONT)
airInfo = ttk.Label(self, text = file, font = NORMAL_FONT)
exitButton = ttk.Button(self, text = "Return to Menu", command = lambda: controller.show_frame(StartPage))
#Placement
airLabel.pack(pady = 10, padx = 10)
airInfo.pack()
exitButton.pack()
上面写着:
exitButton = ttk.Button(self, text = "Return to Menu", command = lambda: controller.show_frame(StartPage))
我希望能够用命令替换StartPage以转到上一个窗口。根据我的帧切换实现,这里有一个可能的实现: 它现在有点老套,但基本上是这样的想法,
goto\u three
调用FrameThree
实例的return\u
,这会改变其按钮上的命令
以返回到调用它的帧
import Tkinter as tk
class BaseFrame(tk.Frame):
"""An abstract base class for the frames that sit inside PythonGUI.
Args:
master (tk.Frame): The parent widget.
controller (PythonGUI): The controlling Tk object.
Attributes:
controller (PythonGUI): The controlling Tk object.
"""
def __init__(self, master, controller):
tk.Frame.__init__(self, master)
self.controller = controller
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create the widgets for the frame."""
raise NotImplementedError
def goto_three(self):
self.controller.frames[FrameThree].return_to(self.__class__)
self.controller.show_frame(FrameThree)
class FrameOne(BaseFrame):
"""First page."""
def create_widgets(self):
"""Create the base widgets for the frame."""
self.button_two = tk.Button(self,
anchor=tk.W,
command=lambda: self.controller.show_frame(FrameTwo),
padx=5,
pady=5,
text="Two")
self.button_two.grid(padx=5, pady=5, sticky=tk.W+tk.E)
self.button_three = tk.Button(self,
anchor=tk.W,
command=self.goto_three,
padx=5,
pady=5,
text="Three")
self.button_three.grid(padx=5, pady=5, sticky=tk.W+tk.E)
class FrameTwo(BaseFrame):
"""Second page."""
def create_widgets(self):
"""Create the base widgets for the frame."""
self.button_one = tk.Button(self,
anchor=tk.W,
command=lambda: self.controller.show_frame(FrameOne),
padx=5,
pady=5,
text="One")
self.button_one.grid(padx=5, pady=5, sticky=tk.W+tk.E)
self.button_three = tk.Button(self,
anchor=tk.W,
command=self.goto_three,
padx=5,
pady=5,
text="Three")
self.button_three.grid(padx=5, pady=5, sticky=tk.W+tk.E)
class FrameThree(BaseFrame):
"""Third page."""
def create_widgets(self):
"""Create the base widgets for the frame."""
self.button_two = tk.Button(self,
anchor=tk.W,
command=lambda: self.controller.show_frame(FrameTwo),
padx=5,
pady=5,
text="Two")
self.button_two.grid(padx=5, pady=5, sticky=tk.W+tk.E)
self.button_one = tk.Button(self,
anchor=tk.W,
command=lambda: self.controller.show_frame(FrameOne),
padx=5,
pady=5,
text="One")
self.button_one.grid(padx=5, pady=5, sticky=tk.W+tk.E)
self.button_back = tk.Button(self,
anchor=tk.W,
padx=5,
pady=5,
text="Back")
self.button_back.grid(padx=5, pady=5, sticky=tk.W+tk.E)
def return_to(self, frame):
self.button_back.config(command=lambda: self.controller.show_frame(frame))
class PythonGUI(tk.Tk):
"""The main window of the GUI.
Attributes:
container (tk.Frame): The frame container for the sub-frames.
frames (dict of tk.Frame): The available sub-frames.
"""
def __init__(self):
tk.Tk.__init__(self)
self.title("Python GUI")
self.create_widgets()
self.resizable(0, 0)
def create_widgets(self):
"""Create the widgets for the frame."""
# Frame Container
self.container = tk.Frame(self)
self.container.grid(row=0, column=0, sticky=tk.W+tk.E)
# Frames
self.frames = {}
for f in (FrameOne, FrameTwo, FrameThree): # defined subclasses of BaseFrame
frame = f(self.container, self)
frame.grid(row=2, column=2, sticky=tk.NW+tk.SE)
self.frames[f] = frame
self.show_frame(FrameOne)
def show_frame(self, cls):
"""Show the specified frame.
Args:
cls (tk.Frame): The class of the frame to show.
"""
self.frames[cls].tkraise()
if __name__ == "__main__":
app = PythonGUI()
app.mainloop()
exit()