473,578 Members | 3,125 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tkFileDialog closes main application

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,m aster):
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,text variable = self.enttxt,wid th = 50)
self.e1.grid(ro w = 0,column = 1,columnspan = 3,sticky = W,padx
= 5,pady = 5)

btn = Button(self,tex t='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.as kopenfilename(t itle='Choose File',
initialdir=file path,
filetypes=filef ormats,
parent = self)
if retval:
self.enttxt.set (os.path.abspat h(retval))

def main():
root = Tk()
root.withdraw()
root.title('tes t')
root.configure( bg='gray')
app = App(root)
app.pack()
root.update()
root.deiconify( )

root.mainloop()
if __name__ == '__main__':
main()
Dec 20 '06 #1
4 2110
On Wed, 20 Dec 2006 18:37:10 +0100, mdmdmd <mt*****@yahoo. comwrote:
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,m aster):
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,text variable = self.enttxt,wid th =50)
self.e1.grid(ro w = 0,column = 1,columnspan = 3,sticky =W,padx
= 5,pady = 5)

btn = Button(self,tex t='Browse ...',width = 12,
command = self.browse)
btn.grid(row = 0,column = 4,sticky=W,padx =5,pady=5)
master.protocol ('WM_DELETE_WIN DOW', self.doQuit)

def doQuit(self):
print 'Closed by the WM!'
self.quit()
>
def browse(self):
fileformats = [('Text File ','*.csv'),
('All Files ','*.*')]

retval = tkFileDialog.as kopenfilename(t itle='Choose File',
initialdir=file path,
filetypes=filef ormats,
parent = self)
if retval:
self.enttxt.set (os.path.abspat h(retval))

def main():
root = Tk()
root.withdraw()
root.title('tes t')
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.zmz 5(17l8(%,5.Z*(9 3-965$l7+-'])"
Dec 21 '06 #2
Eric Brunel wrote:
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/
Dec 21 '06 #3
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 <mt*****@yahoo. comwrote:
>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,m aster):
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,text variable = self.enttxt,wid th = 50)
self.e1.grid(ro w = 0,column = 1,columnspan = 3,sticky =
W,padx = 5,pady = 5)

btn = Button(self,tex t='Browse ...',width = 12,
command = self.browse)
btn.grid(row = 0,column = 4,sticky=W,padx =5,pady=5)


master.protocol ('WM_DELETE_WIN DOW', self.doQuit)

def doQuit(self):
print 'Closed by the WM!'
self.quit()
>>
def browse(self):
fileformats = [('Text File ','*.csv'),
('All Files ','*.*')]

retval = tkFileDialog.as kopenfilename(t itle='Choose File',
initialdir=file path,
filetypes=filef ormats,
parent = self)
if retval:
self.enttxt.set (os.path.abspat h(retval))

def main():
root = Tk()
root.withdraw()
root.title('tes t')
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
Dec 22 '06 #4
On Thu, 21 Dec 2006 22:37:37 +0100, James Stroud <js*****@mbi.uc la.edu
wrote:
Eric Brunel wrote:
>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.
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.
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz 5(17l8(%,5.Z*(9 3-965$l7+-'])"
Dec 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1889
by: Daylor | last post by:
i have 1 main winapplication. 3 services ,each in dll. each service is created for telephony user. each service is destroyed when the telephony user is hangUp. is there a way to recompile the dll service (to add for example 1 line of code/fix smale bug/small change ) while the main winappilcation is running.
1
1312
by: Andy | last post by:
Hi! I have a main application here and developed 2 modules as DLL files, which will be updated from time to time. The main application ships without the modules so i want to be able, to put the module DLLs in application folder and call them from the main application. My question: is it possible to use my modules like that? The DLL files...
6
1908
by: Jay Douglas | last post by:
Greetings, I'm hoping somebody can help me come up with a quick software architecture solution to address the following issue: I need to come up with an Winforms (or something similar) application that multiple users will use over a network. I would like the client machines to actually use an application that sits on a master server...
5
1423
by: Anand Ganesh | last post by:
Hi All, I need some help. I am sort of not sure how to approach this problem. I have a MAINPROGRAM. This is the core application. I have asked two of my staff to developed two different Windows Controls. Say ControlA.dll and ControlB.dll. As usual I know how to add these .dlls as the reference and then start using
0
1211
by: Rajiv Das | last post by:
Environment: Visual Studio 2005, Beta 2 ..Net 2.0 Windows XP, SP2 C# Generics ------------------------------- Hi, I have a Windows Form whose contents I would like to dynamically change. I
0
1013
by: calderara serge | last post by:
Dear All, I am building an application with VB.Net that is starting an external process from my application code by using the process object. The problem I get is that the application that I need to start from my code can takes some times to be really in full real mode. In other words, my main application should continue its next code...
7
1956
by: Byron Hopp | last post by:
Is there a way to get a reference to the main application window object in vb.net? Like an MDI app, I would like to get to the MDI Shell window. Thanks, Byron...
0
1019
by: Steven | last post by:
When a user is redirected to our app thru a _blank target hyperlink, everything seems to work fine until he downloads a file from our site. This is the code used for downloading: Dim fso As New Scripting.FileSystemObject() Dim TS As Object Dim strFile As String Dim strFileSave As String Dim b As Byte Dim MyFileInfo As FileInfo
3
1581
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2.0, Win2k server for a Windows applcaiton. I have a main application where there is a a form and the codes that I would like to make it reuseable by other applications. 1. I already have a classlibray project but I don't think I can include a "form" in it, right? 2. So, do I make this form into a dll so other...
0
7847
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7778
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8290
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7874
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5342
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1389
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.