tkFileDialog closes main application

Eric Brunel eric_brunel at despammed.com
Thu Dec 21 03:16:55 EST 2006


On Wed, 20 Dec 2006 18:37:10 +0100, mdmdmd <mt_nich at yahoo.com> wrote:

> Hello,
>
> I wish to collect 4 files from a user.  So I have decided to use  
> tkFileDialog askopenfilename.  My problem is that after a few file  
> selections the root window is destroyed (the whole program just  
> dissappears)

I tested the code below on Linux with Python 2.1 and tcl/tk 8.3.4 and it  
works perfectly.

> I have created a simple example and was able to reproduce the same thing  
> with this.  I've just started using tkinter so I have no idea what I may  
> be doing wrong.  If anyone has any ideas please let me know.
>
> If you run the following code, just click the Browse button, and select  
> a file.  Do this repeatedly and for me after the sixth or seventh time  
> the window shuts down.

Is there any error when this happens? Have you tried running your script  
 from a DOS command window?

> BTW, I'm using python 2.4 on Windows XP.  Thank you for any help.

How do you select your files? I occasionally see problems on Windows when  
a window is closed via a double-click: the last 'button release' event for  
the double-click is not consumed by the dialog, but sent to the window  
behind it. Could it be what happens? If you select your files with a  
double-click and if the mouse cursor just happens to be on the close  
button for your main window behind it, you may involuntarily close the  
main window when selecting the file. Please try to select the files and  
then press the 'Open' button to see if the problem still happens.

Here is a tiny modification to your code to print a message when the  
window is closed via its close button:

> ################################################################################
>
> from Tkinter import *
> import Pmw
> import tkFileDialog
> import os.path
>
> filepath = 'C:\\Documents and Settings\\admin\\Desktop\\'
>
> class App(Frame):
>      def __init__(self,master):
>          Frame.__init__(self, master, bg='gray')
>          self.enttxt = StringVar()
>
>          lbl = Label(self,text='File 1:')
>          lbl.grid(row = 0,column = 0,sticky = W,padx = 5,pady = 5)
>
>          self.e1 = Entry(self,textvariable = self.enttxt,width = 50)
>          self.e1.grid(row = 0,column = 1,columnspan = 3,sticky = W,padx  
> = 5,pady = 5)
>
>          btn = Button(self,text='Browse ...',width = 12,
>                       command = self.browse)
>          btn.grid(row = 0,column = 4,sticky=W,padx=5,pady=5)

            master.protocol('WM_DELETE_WINDOW', self.doQuit)

        def doQuit(self):
            print 'Closed by the WM!'
            self.quit()
>
>      def browse(self):
>          fileformats = [('Text File ','*.csv'),
>                         ('All Files ','*.*')]
>
>          retval = tkFileDialog.askopenfilename(title='Choose File',
>                                                initialdir=filepath,
>                                                filetypes=fileformats,
>                                                parent = self)
>          if retval:
>              self.enttxt.set(os.path.abspath(retval))
>
> def main():
>      root = Tk()
>      root.withdraw()
>      root.title('test')
>      root.configure(bg='gray')
>      app = App(root)
>      app.pack()
>      root.update()
>      root.deiconify()
>
>      root.mainloop()
>
>
> if __name__ == '__main__':
>      main()

BTW, why do you create a sub-class of Frame for your application? Why not  
create a sub-class of Tk instead?

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list