在Tkinter中切换帧

时间:2016-04-01 15:38:21

标签: python-3.x tkinter

我遇到了这个很棒的教程 - https://www.youtube.com/watch?v=jBUpjijYtCk&index=4&list=PLQVvvaa0QuDclKx-QpC9wntnURXVJqLyk

它的第3部分,它基本上只是能够通过按下按钮来交换框架。我几乎遵循了所有步骤,一切正常,直到大约7.06

这是代码 -

import Tkinter as tk

class MainApp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

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

            frame = F(container,self)

            self.frames[F] = frame

            frame.grid(row = 0, column = 0, sticky = "w")

        self.show_frame(StartPage)

    def show_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)

        lbl = tk.Label(self, text = "Start Page", font =("Helvetica",12,"bold"))
        lbl.pack()

        btn = tk.Button(self,text = "Button to next frame", font = ("Helvetica",12,"bold")
                    ,command =lambda: controller.show_frame(PageOne))
    btn.pack()

class PageOne(tk.Frame):
    def __int__(self,parent,controller):
        tk.Frame.__init__(self,parent)

        lbl = tk.Label(self, text = "page 1", font =("Helvetica",12,"bold"))
        lbl.pack()

        btn1 = tk.Button(self,text = "Button to pageOne", font = ("Helvetica",12,"bold")
                    ,command =lambda: controller.show_frame(StartPage))
        btn1.pack()


app = MainApp()
app.mainloop()

实现循环时会抛出错误 -

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2090, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-use"

我仔细仔细检查了代码并且看不出任何差异,所以我不完全确定它的含义或原因是什么?或为什么在视频中没有发生同样的事情,任何想法?

1 个答案:

答案 0 :(得分:0)

您在PageOne中拼错了__init____int__,这意味着正在运行默认的__init__。默认的__init__方法的第二个参数是use而不是controller,这就是您遇到特定错误的原因。

顺便说一句,该教程中的代码源于stackoverflow答案,然后由该教程的创建者复制。您可能会发现原始答案和一些相关答案很有用:

https://stackoverflow.com/a/7557028/7432