在tkinter中的两帧之间切换

我已经build立了我的第一个几个脚本与一个很好的小graphics用户界面,如教程显示了我,但他们都没有解决如何做一个更复杂的程序。

如果你有一个“开始菜单”的东西,对于你的开始屏幕,并根据用户的select,你移动到程序的不同部分,并适当地重画屏幕,这样做的优雅方式是什么?

是否只是.destroy()的“开始菜单”框架,然后创build一个新的填充与另一部分的部件? 当他们按下后退button时,逆转这个过程?

一种方法是将框架堆叠在一起,然后您可以在堆叠顺序中简单地将一个在另一个之上。 顶部的那个将是可见的那个。 如果所有的框架都是相同的大小,这个效果最好,但是有一点工作可以使它适用于任何大小的框架。

这里有一个人为的例子来向你展示一般的概念:

 import tkinter as tk # python 3 from tkinter import font as tkfont # python 3 #import Tkinter as tk # python 2 #import tkFont as tkfont # python 2 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=lambda: controller.show_frame("StartPage")) button.pack() if __name__ == "__main__": app = SampleApp() app.mainloop() 

如果您发现在类中创build实例的概念令人困惑,或者在构build过程中不同的页面需要不同的参数,则可以分别显式调用每个类。 循环主要用来说明每个类是相同的。

例如,要单独创build类,可以使用以下命令删除循环( for F in (StartPage, ...)

 self.frames["StartPage"] = StartPage(parent=container, controller=self) self.frames["PageOne"] = PageOne(parent=container, controller=self) self.frames["PageTwo"] = PageTwo(parent=container, controller=self) self.frames["StartPage"].grid(row=0, column=0, sticky="nsew") self.frames["PageOne"].grid(row=0, column=0, sticky="nsew") self.frames["PageTwo"].grid(row=0, column=0, sticky="nsew") 

随着时间的推移,人们已经使用此代码(或复制此代码的在线教程)作为起点提出其他问题。 您可能想要阅读这些问题的答案:

  • 理解Tkinter中的父类和控制器__init__
  • Tkinter的! 了解如何切换帧
  • 如何从一个类获取可变数据
  • 如何访问tkinter python中不同类的variables3
  • 每当tkinter显示一个帧时,我将如何制作一个方法
  • Tkinter框架调整
  • Py2.7:Tkinter在单独的文件中有页面的代码

这是另一个简单的答案,但不使用类。

 from tkinter import * def raise_frame(frame): frame.tkraise() root = Tk() f1 = Frame(root) f2 = Frame(root) f3 = Frame(root) f4 = Frame(root) for frame in (f1, f2, f3, f4): frame.grid(row=0, column=0, sticky='news') Button(f1, text='Go to frame 2', command=lambda:raise_frame(f2)).pack() Label(f1, text='FRAME 1').pack() Label(f2, text='FRAME 2').pack() Button(f2, text='Go to frame 3', command=lambda:raise_frame(f3)).pack() Label(f3, text='FRAME 3').pack(side='left') Button(f3, text='Go to frame 4', command=lambda:raise_frame(f4)).pack(side='left') Label(f4, text='FRAME 4').pack() Button(f4, text='Goto to frame 1', command=lambda:raise_frame(f1)).pack() raise_frame(f1) root.mainloop()