[Tkinter-discuss] TclError when destroying parent windows

Guilherme Polo ggpolo at gmail.com
Sun Jun 29 16:09:07 CEST 2008


On Sun, Jun 29, 2008 at 9:22 AM, Jusa <sj.m at iloinen.net> wrote:
>
>
>
> Mick O'Donnell wrote:
>>
>> Hi Jusa,
>>
>>   Can you post some sample code which causes the problem?
>>
>> Mick
>> _______________________________________________
>> Tkinter-discuss mailing list
>> Tkinter-discuss at python.org
>> http://mail.python.org/mailman/listinfo/tkinter-discuss
>>
>>
>
> Hi Mick,
>
> It's rather lengthy but here goes (the error-causing lines are at the
> bottom):
>
>
> from Tkinter import *
>
> class MyDialog(Toplevel):
>    '''
>    Base class for custom dialogs.
>    '''
>
>    def __init__(self, master, title, size):
>        Toplevel.__init__(self, master)
>
>        self.title(title)
>        self.accepted = False
>
>        x = self.master.winfo_rootx() + self.master.winfo_width() / 2
>        y = self.master.winfo_rooty() + self.master.winfo_height() / 2
>
>        self.columnconfigure(0, weight=1)
>        self.rowconfigure(0, weight=1)
>
>        self.wndSize = size
>        self.geometry('+%d+%d' % (size[0], size[1]))
>
>    def display(self):
>        '''
>        Displays the dialog and returns the parameters polled from the user.
>        '''
>
>        self.build()
>        self.transient(self.master)
>        self.grab_set()
>        self.protocol('WN_DELETE_WINDOW', self.onCancel)
>        x = self.master.winfo_rootx() + self.master.winfo_width() / 2
>        y = self.master.winfo_rooty() + self.master.winfo_height() / 2
>        self.geometry('+%d+%d' % (x, y))
>        self.wait_window(self)
>        return self.output()
>
>    def build(self):
>        '''
>        Override: use this to construct the widgets on the dialog.
>        '''
>
>    def output(self):
>        '''
>        Override: use this to return any dialog-specific parameters at the
>        end of display()
>        '''
>
>        return None
>
>    def validate(self):
>        '''
>        Override: use this to validate user input in the dialog.
>        '''
>
>        return True
>
>    def onOk(self, event=None):
>        '''
>        Callback for accepting the dialog.
>        '''
>        if self.validate() == False:
>            return
>
>        self.accepted = True
>        self.withdraw()
>        self.update_idletasks()
>        self.onCancel()
>
>    def onCancel(self, event=None):
>        '''
>        Callback for rejecting the dialog.
>        '''
>        self.master.focus_set()
>        self.master.grab_set()
>        self.destroy()
>
> class MyMessageDialog(MyDialog):
>    '''
>    A simple custom message dialog.
>    '''
>
>    def __init__(self, master, title, message, btnText='Ok', size=(200,
> 80)):
>        MyDialog.__init__(self, master, title, size)
>
>        self.message = message
>        self.btnText = btnText
>
>    def build(self):
>        self.frame = Frame(self, padx=7, pady=7)
>        self.frame.pack(fill=BOTH, expand=YES)
>
>        self.messageLbl = Label(self.frame, text=self.message,
> wraplength=self.wndSize[0]-7*2, justify=CENTER)
>        self.messageLbl.pack(fill=BOTH, expand=YES)
>
>        self.yesBtn = Button(self.frame, text=self.btnText,
> command=self.onOpenError)
>        self.yesBtn.pack(pady=5)
>        self.bind('<Return>', self.onOk)
>        self.bind('<Escape>', self.onOk)
>
>    def onOpenError(self):
>        dlg = MyMessageDialog(self, 'Watch out', 'En error occurs when you
> close the window \'Hello\'.', 'Don\'t press this')
>        dlg.display()
>

Here, supposing you closed the "Hello" window, self (representing the
"parent" dialog) would have been destroyed by now.
Then next:

>        #ERROR:
>        dlg = MyMessageDialog(self, 'Error', 'This dialog causes the
> error.')
>        dlg.display()
>        #/ERROR

You try to use "self" as the parent, which is not valid anymore. You
could change this to self.master, which still exists.

>
> root = Tk()
>
> hello = MyMessageDialog(root, 'Hello', 'Good day!', 'Open error')
> params = hello.display()
>
> root.mainloop()
>
> --
> View this message in context: http://www.nabble.com/TclError-when-destroying-parent-windows-tp18178579p18180082.html
> Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>



-- 
-- Guilherme H. Polo Goncalves


More information about the Tkinter-discuss mailing list