(widgetName,self._w)+ extra + self._options(cnf))_ tkinter.TclError:未知选项“-parent”

时间:2017-11-04 12:33:13

标签: python python-3.x tkinter

我正在尝试在Tkinter上创建修订指南,基本工作正常,但是当我试图在框架内放置框架时(按下物理将显示另外两个按钮,每个按钮自己显示一个框架)我的Python代码,显示错误,所以我创建了一个新的文件,其中物理本身是起始页面,但我在运行它时遇到此错误,我不明白什么是错的,我已经检查了其他答案和问题,但我仍然不明白我做错了什么

  

(widgetName,self._w)+ extra + self._options(cnf))   _tkinter.TclError:未知选项“-parent”

这是我的代码

class StartPage(tk.Frame):
def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    button1 = tk.Button(self, text="Physics Equations", command=lambda: controller.show_frame("PhysicsEquations"),bg="blue",fg="white",font=(None, 23, "bold"), height=2, width=11).grid(row=0,column=0,padx=5,pady=5)
    button2 = tk.Button(self, text="Physics Definitions", command=lambda: controller.show_frame("PhysicsDefinitions"),bg="green",fg="white",font=(None, 23, "bold"), height=2, width=11).grid(row=0,column=1,padx=5,pady=5)
    button = tk.Button(self, text="Go to the start page",command=lambda: controller.show_frame("StartPage"),fg="white",bg="blue",width=10,font=(None,20,"bold"))
    button.grid()

class PhysicsEquations(tk.Frame):
def clickPhysicsEquations(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    label = tk.Label(self, text="This is Physics", font=controller.title_font,fg="white", bg="blue")
    label.pack(side="top", fill="x", pady=10)
    label1 = tk.Label(self, text=' ',bg="blue")
    label1.pack()
    try:
        file=open("physics_EQUATIONS.txt","r")
        data=file.read()
        file.close()
    except IOError:
        file=open("physics_EQUATIONS.txt","w+")
        file.write("Distance =  Speed x Time,")
        data=file.read()
        file.close()
    physics_equations=data.split(',') 
    temp=random.choice(physics_equations)
    used=[]
    used.append(temp)
    physics_equations.remove(temp)
    label1.configure(text=temp)

class PhysicsDefinitions(tk.Frame):
def clickPhysicsDefinitions(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    S = tk.Scrollbar(self)
    T = tk.Text(self, height=2 , width =100,bg="blue")
    S.pack(side="right", fill="y")
    T.pack(side="left", fill="y")
    S.config(command=T.yview)
    T.config(yscrollcommand=S.set)
    definitions="""Acceleration: Rate of change of velocity (affected by mass and force)."""
    T.insert(tk.END, definitions)
    button2=tk.Button(self, text="Definitions",command=clickPhysicsDefinitions,fg="white",bg="blue",width=10,font=(None,20,"bold"))
    button2.pack()
    button = tk.Button(self, text="Go to the start page",command=lambda: controller.show_frame("StartPage"),fg="white",bg="blue",width=10,font=(None,20,"bold"))
    button.pack()

if __name__ == "__main__":
app = SampleApp()
app.mainloop()

很抱歉,如果我在提出这个问题时犯了任何错误,请第一时间询问。

1 个答案:

答案 0 :(得分:0)

看看这两个类定义:

起始页:

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

PhysicsDefinitions:

class PhysicsDefinitions(tk.Frame):
    def clickPhysicsDefinitions(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        ...

注意区别?一个有__init__,一个没有。不受欢迎的取决于基类中__init__的实现。您将其称为PhysicsDefinitions(parent=self, controller=self),其变为Frame(parent=..., controller=...)Frame不接受parentcontroller的命名参数。

如果要传递对基类无效的参数,则必须创建一个接受这些参数的自定义__init__,如第一个示例所示。