tkFileDialog closes main application

2006-12-20 Thread mdmdmd
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 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.

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



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)

 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()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tkFileDialog closes main application

2006-12-21 Thread mdmdmd
Thanks for the reply.

I used your modified code to test.  I ran the code on Windows Python 2.4 
tcl/tk 8.4.  When I opened the ui I:
1) click browse button
2) file dialog opens and I double click the file.  When I do this, the 
selected file path is entered in Entry field. I don't need to close 
dialog, it closes automatically.
3) click browse button again
4) double click file
5) repeat

sometimes it only takes one time to kill root, sometimes a few more. 
After I killed root with the double clicks I then tried with a click 
browse - select file - click open button very slowly making sure I 
didn't double click and the same thing happened - root was destroyed.

BTW, with your modified code, the "Closed by the WM" was not displayed 
to console.

Hmmm?  I think this must just be peculiar to Windows.  Can any other 
windows users test to see if they can reproduce?

thanks again for your response.

Eric Brunel wrote:
> On Wed, 20 Dec 2006 18:37:10 +0100, mdmdmd <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list