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