Tkinter:如何在元组中显示下一帧

时间:2019-03-30 04:18:59

标签: python tkinter

我正在尝试为tkinter按钮编写一种方法,该方法将遍历一个帧的元组并在每次单击下一个按钮时显示下一个帧。目的是编写函数,以便我只需要传递“ self”(对于当前可见的框架对象)作为参数。

我已经看过一些示例,这些示例允许我将新框架作为参数传递给函数来切换到新框架。目前,我已经按照这些思路实施了一些临时工作,直到找到解决当前问题的方法为止。我不确定要包含多少代码,但已将其配对,以使其仍能以最少的问题运行。

元组App.frames和App.next_frame()中的else块是问题区域。

class App(tk.Frame):
    def __init__(self, master = None):
        super().__init__(master)
        self.master = master
        self.pack()

        #### Add any new pages to this tuple ####
        self.frames = (StartPage, Page2)
        self.current_frame = None
        self.next_frame(self.current_frame)

    def next_frame(self, cframe_class):
        '''Set the visible page to the next frame in the frames tuple.
        Pass self to the cframe_class argument'''
        if self.current_frame == None:
            '''If the application has just started, this block will run
            in order to set the current frame to the start page.'''
            self.current_frame = StartPage(self)
            self.current_frame.pack()
        else:         ##### Problem #####
            cfi = self.frames.index(cframe_class)
            #### "ValueError: tuple.index(x): x not in tuple" ####
            cfi += 1
            self.current_frame.destroy()
            self.current_frame = self.frames[cfi]
            self.current_frame.pack()

class StartPage(tk.Frame):
    def __init__(self, master = None):
        super().__init__(master)
        tk.ttk.Button(self, text = "Test Next",
                        command = lambda: master.next_frame(self)).pack()

class Page2(tk.Frame):
    def __init__(self, master = None):
        super().__init__(master)
        tk.ttk.Label(self, text = "This is Page 2").pack()

def main():
    root = tk.Tk()
    app = App(root)
    app.mainloop()    #app.mainloop() or root.mainloop()?

if __name__ == "__main__":
    main()

调试器在cfi变量行上抛出“ ValueError:tuple.index(x):x不在tuple中” 。当我单击下一步按钮后逐步执行该功能时,调试器告诉我参数“ cframe_class”的ID为:

<__main__.StartPage object .!app.!startpage>

我了解该行的第一部分,但不确定如何解释 object。!app。!startpage> 部分。看起来应该这样看:“起始页是从NOTapp继承的对象,不是起始页”,但我不知道为什么会这样。

元组中的StartPage对象的ID为:

<class '__main__.StartPage'>

1 个答案:

答案 0 :(得分:1)

也许您可以使用存储为属性的索引来跟踪当前帧,而不用传递参数。

import tkinter as tk
from tkinter import ttk

class App(tk.Frame):
    def __init__(self, master = None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.cfi = 0
        #### Add any new pages to this tuple ####
        self.frames = (StartPage(self), Page2(self))
        self.current_frame = None
        self.next_frame()

    def next_frame(self):
        '''Set the visible page to the next frame in the frames tuple.
        Pass self to the cframe_class argument'''
        if self.cfi == 0:
            '''If the application has just started, this block will run
            in order to set the current frame to the start page.'''
            self.current_frame = self.frames[0]
            self.current_frame.pack()
            self.cfi +=1
        else:
            self.current_frame.pack_forget()
            try:
                self.current_frame = self.frames[self.cfi]
            except IndexError:
                self.current_frame = self.frames[0]
                self.cfi = 0
            self.current_frame.pack()
            self.cfi += 1

class StartPage(tk.Frame):
    def __init__(self, master = None):
        super().__init__(master)
        tk.Button(self, text = "Test Next", command = master.next_frame).pack()

class Page2(tk.Frame):
    def __init__(self, master = None):
        super().__init__(master)
        tk.Button(self, text = "This is Page 2",command=master.next_frame).pack()

def main():
    root = tk.Tk()
    app = App(root)
    app.mainloop()    #app.mainloop() or root.mainloop()?

if __name__ == "__main__":
    main()