2

Topleveldistroyクラス内からアクセスするのが難しいメソッドがあります。

このコードは機能します:

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'
4

1 に答える 1

2

それは、「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()
于 2013-08-08T14:18:18.270 に答える