gpt4 book ai didi

python - 从 Toplevel 访问继承的方法

转载 作者:太空宇宙 更新时间:2023-11-04 03:56:31 26 4
gpt4 key购买 nike

Toplevel 有一个方法 distroy,我很难从类内部访问它。

此代码有效:

top = Toplevel()
Message(top, text="bla bla bla...").pack()
Button(top, text="Dismiss", command=top.distroy).pack()
top.mainloop()

这不是:

from Tkinter import Toplevel,Message,Button,mainloop

class Demo(Toplevel):
def __init__(self,title,message,master=None):
Toplevel.__init__(self,master)
self.title = title
msg = Message(self,text=message)
msg.pack()
button = Button(self, text="Dismiss", command=self.distroy)
button.pack()

if __name__ == '__main__':
t1 = Demo("First Toplevel", "some random message text... goes on and on and on...")
t2 = Demo("No, I don't know!", "I have no idea where the root window came from...")

mainloop()

错误信息:

Traceback (most recent call last):
File "C:\Users\tyler.weaver\Projects\python scripts\two_toplevel.pyw", line 16, in <module>
t1 = Demo("First Toplevel", "some random message text... goes on and on and on...")
File "C:\Users\tyler.weaver\Projects\python scripts\two_toplevel.pyw", line 12, in __init__
button = Button(self, text="Dismiss", command=self.distroy)
AttributeError: Demo instance has no attribute 'distroy'

最佳答案

那是因为你把“destroy”拼错了:

from Tkinter import Toplevel,Message,Button,mainloop

class Demo(Toplevel):
def __init__(self,title,message,master=None):
Toplevel.__init__(self,master)
self.title = title
msg = Message(self,text=message)
msg.pack()
# Use "self.destroy", not "self.distroy"
button = Button(self, text="Dismiss", command=self.destroy)
button.pack()

if __name__ == '__main__':
t1 = Demo("First Toplevel", "some random message text... goes on and on and on...")
t2 = Demo("No, I don't know!", "I have no idea where the root window came from...")

mainloop()

关于python - 从 Toplevel 访问继承的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18128346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com