tkFileDialog closes main application

M

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()
 
E

Eric Brunel

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
 
J

James Stroud

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

The short answer is that inhereting from Frame will allow embedding of
the application in another application. A Tk() can not be embedded like
this. Tk is appropriately instantiated if (and only if) __name__ ==
"__main__" here, allowing the App to run as the "main" application here.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
M

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 said:
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
 
E

Eric Brunel

The short answer is that inhereting from Frame will allow embedding of
the application in another application. A Tk() can not be embedded like
this. Tk is appropriately instantiated if (and only if) __name__ ==
"__main__" here, allowing the App to run as the "main" application here.

So I rephrase my question: will this application ever need to be embedded
into another one?

There are problems with this way of doing things, especially with menus:
if you have to define a menu bar, you just can't attach it to a Frame; you
have to have a Tk or Toplevel instance. So basically you're stuck: you
can't make your application embeddable anymore. So if you actually need to
have a graphical component / mega-widget that has a chance to be embedded
in something else, sub-classing Frame is the way to go. If you don't,
you'll have far less trouble if you sub-class Tk or Toplevel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,750
Members
45,545
Latest member
rapter____0

Latest Threads

Top