[Tkinter-discuss] tkFileDialog: shuts down root window

mopman dnichols32 at comcast.net
Thu Dec 21 01:44:56 CET 2006


Hello,

I'm using python tkinter to create a GUI.  I wish to collect 4 files from a
user so for this part I created a GUI that has a Label, Entry  and Button. 
To get the file I use tkFileDialog askopenfilename.  My problem is that
after a few file selections using the tkFileDialog, the root window is
destroyed (the whole program just dissappears). 

I have created a simpler 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.  The file selected is displayed in the Entry field.  Click the Browse
button again and select another file or the same file.  Do those steps
several times and for me after the sixth or seventh time the window shuts
down.  If I restart the program, it may only take one or two times for the
root window to be destroyed.

BTW, I'm using python 2.4 on Windows XP.  Does anyone have any ideas why
this may be happening?  

################################################################################

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() 
-- 
View this message in context: http://www.nabble.com/tkFileDialog%3A-shuts-down-root-window-tf2863117.html#a8000939
Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.



More information about the Tkinter-discuss mailing list