【问题标题】:(widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: unknown option "-parent"(widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: 未知选项“-parent”
【发布时间】:2017-11-04 12:33:13
【问题描述】:

我正在尝试在 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()

对不起,如果我在问这个问题时有任何错误,请第一次问。

【问题讨论】:

  • 创建三个框架时,您提供了两个命名参数:parentcontrollerStartPage 被定义为采用这两个参数,因此它可以工作。另外两个没有定义自己的__init__() 方法,所以它们继承了Frame 的构造函数——它不知道任何这样的参数。看来您在PhysicsEquationsPhysicsDefinitions 中定义的一种方法应该是__init__,但您却给了它们一些无意义的名称。
  • 请将代码量减少到minimal reproducible example。例如,我们真的需要接近 50 行的文本来重现问题吗?我们真的需要三页而不是一两页吗?
  • 对不起,问题的长度,
  • 谢谢 Jason,效果很好,我知道你来自哪里,它们被称为“click....”的原因是因为这种代码变体是我的第 5 次尝试,类似这样,所以我在不更改名称的情况下复制了功能,再次感谢您

标签: python python-3.x tkinter


【解决方案1】:

看看这两个类定义:

起始页:

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

物理定义:

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__ 来接受这些参数,就像在第一个示例中一样。

【讨论】:

  • 感谢您的回答,它工作得很好,再次抱歉帖子的长度,我还在习惯这个。
猜你喜欢
  • 1970-01-01
  • 2020-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多