2011-09-25 131 views
52

我已经用它们上好的小GUI构建了我的第一个脚本,就像教程向我展示的那样,但是他们都没有解决为更复杂的程序做什么。在tkinter中的两帧之间切换

如果你有一个'开始菜单',对于你的开始屏幕,当用户选择你移动到程序的不同部分并适当重画屏幕时,这样做的优雅方式是什么?

只有一个.destroy()'开始菜单'框架,然后创建一个新的部件填充另一部件的小部件?当他们按下后退按钮时可以逆转这个过程?

回答

88

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

注意:这个工作,所有的部件的页面必须有一个页面(即:self)或后代作为父(或主,这取决于你喜欢的术语)。

这是一个有点做作的例子向您展示的一般概念:

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() 

start pagepage 1page 2

如果你发现一个类创建实例混淆的概念,或者不同的页面需要不同的参数在建设过程中,你可以分别明确地调用每个类。循环主要用于说明每个类是相同的。

例如,单独创建类可以消除环路(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") 

随着时间的推移人都问过使用此代码(或复制本的在线教程其他问题代码)为起点,你可能会想读这些问题的答案:

+2

哇,非常感谢。就像一个学术问题而不是实际问题一样,在开始变得迟缓和反应迟钝之前,您需要将多少页面隐藏在彼此之上? –

+2

我不知道。可能有成千上万。测试会很容易。 –

+0

@BryanOakley @BryanOakley很好的回答,如果我有一个树视图,例如pageTwo,并且想要在页面中填写一些数据时更新,我怎样才能在pageTwo中调用方法或函数来更新树视图。 – diego

16

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

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() 
+0

我们如何显示大尺寸的标签,按钮。看来框架的宽度和高度都很小。即使在使用root.geometry()增加窗口尺寸之后 – velpandian