Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103418 > unrolled thread

[Newbie] Tkinter Question

Started byWildman <best_lay@yahoo.com>
First post2016-02-23 16:19 -0600
Last post2016-02-25 11:13 -0600
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  [Newbie] Tkinter Question Wildman <best_lay@yahoo.com> - 2016-02-23 16:19 -0600
    Re: [Newbie] Tkinter Question Christian Gollwitzer <auriocus@gmx.de> - 2016-02-23 23:37 +0100
    Re: [Newbie] Tkinter Question Chris Kaynor <ckaynor@zindagigames.com> - 2016-02-23 14:41 -0800
    Re: [Newbie] Tkinter Question Wildman <best_lay@yahoo.com> - 2016-02-24 23:13 -0600
      Re: [Newbie] Tkinter Question Randy Day <randy.day@sasktel.netx> - 2016-02-25 11:13 -0600

#103418 — [Newbie] Tkinter Question

FromWildman <best_lay@yahoo.com>
Date2016-02-23 16:19 -0600
Subject[Newbie] Tkinter Question
Message-ID<Ye-dnZQLyOFiR1HLnZ2dnUU7-YPOydjZ@giganews.com>
I am familiar with OO programming but I am new to Python
and Tkinter.  I am working on a gui program that creates
a couple of temporary files.  As part of the Exit button
command they are deleted.  If the program is shut down
using the window close button [X], the exit button code
is not executed and the temporary files are left behind.
That is a serious no-no.

Is there a way to capture close button click to prevent
the program from closing and instead execute a command?
I'm talking about something like the Form_QueryUnload
event in Visual Basic.

Alternately is there a way to just disable the button
or prevent it from being displayed in the first place?

Any help appreciated.

-- 
<Wildman> GNU/Linux user #557453
"Be at war with your vices, at peace with your neighbors,
and let every new year find you a better man."
  -Benjamin Franklin

[toc] | [next] | [standalone]


#103421

FromChristian Gollwitzer <auriocus@gmx.de>
Date2016-02-23 23:37 +0100
Message-ID<naimpd$4pf$1@dont-email.me>
In reply to#103418
Am 23.02.16 um 23:19 schrieb Wildman:
> I am familiar with OO programming but I am new to Python
> and Tkinter.  I am working on a gui program that creates
> a couple of temporary files.  As part of the Exit button
> command they are deleted.  If the program is shut down
> using the window close button [X], the exit button code
> is not executed and the temporary files are left behind.
> That is a serious no-no.
>
> Is there a way to capture close button click to prevent
> the program from closing and instead execute a command?

It's done using the WM_DELETE_WINDOW protocol:

http://stackoverflow.com/questions/111155/how-do-i-handle-the-window-close-event-in-tkinter

Your program could still be killed by another way, e.g. from the task 
manager, which would leave the files behind.

	Christian

[toc] | [prev] | [next] | [standalone]


#103422

FromChris Kaynor <ckaynor@zindagigames.com>
Date2016-02-23 14:41 -0800
Message-ID<mailman.81.1456267296.20994.python-list@python.org>
In reply to#103418
On Tue, Feb 23, 2016 at 2:19 PM, Wildman via Python-list <
python-list@python.org> wrote:

> I am familiar with OO programming but I am new to Python
> and Tkinter.  I am working on a gui program that creates
> a couple of temporary files.  As part of the Exit button
> command they are deleted.  If the program is shut down
> using the window close button [X], the exit button code
> is not executed and the temporary files are left behind.
> That is a serious no-no.
>
> Is there a way to capture close button click to prevent
> the program from closing and instead execute a command?
> I'm talking about something like the Form_QueryUnload
> event in Visual Basic.
>
> Alternately is there a way to just disable the button
> or prevent it from being displayed in the first place?
>
> Any help appreciated.
>

There are a few options involved, however one other case to consider is if
the user force kills the app (or the whole system is shut down improperly,
such as a power loss). In those cases, should the temp files be removed? If
yes, you'll need to use OS features to ensure the files are properly
deleted by letting the OS know they are temporary.

If the temp files need to be removed no matter what, the primary option is
something like
https://docs.python.org/2/library/tempfile.html#tempfile.NamedTemporaryFile
using the delete=True arguments. Alternatively, you can probably use
os.open with O_TEMPORARY (I belive this is the same
as FILE_FLAG_DELETE_ON_CLOSE in the Win32 API, and there is probably
something similar for Linux).

If you only care if the user closes the app properly (no ending the process
on Windows), you can perform the clean-up when the mainloop returns, aka
Tkinter.Tk().mainloop(). Based off other GUI systems, this will only occur
once all windows have been closed. Another option would be to use Python's
atexit library: https://docs.python.org/3/library/atexit.html. atexit has
the advantage that you could register/unregister the handlers when needed,
rather than maintaining your own list to clean-up. Tkinter may also have
some hook you can use to get when it is exiting, but I do not know what
that is. The same limitations regarding OS exits will apply, however.

If you need somewhere in the middle, you can try signal handlers, which,
for some types of external exits, will fire, but it is not certain.

Chris

[toc] | [prev] | [next] | [standalone]


#103478

FromWildman <best_lay@yahoo.com>
Date2016-02-24 23:13 -0600
Message-ID<DumdnVLDRa4MEFPLnZ2dnUU7-fWdnZ2d@giganews.com>
In reply to#103418
On Tue, 23 Feb 2016 16:19:43 -0600, Wildman wrote:

> <snip for brevity>

Thanks to Christian and Chris.  You both gave me much
to think about and to experiment with.  That adds to
my on-going learning experience.

This is the first thing I tried:

The Exit button has this:  command=self.quit

Then I have this:

def clean_up():
     # delete temp files

root = Tk()
app = Window(root)
root.protocol("DELETE_WINDOW", clean_up)
root.mainloop()

And I also tried:

root.wm_protocol("WM_DELETE_WINDOW", clean_up)

Both worked when the window [X] button was clicked but
neither worked when the Exit button was clicked.
However, when I removed the protocol statement and
replaced with this, it worked for both:

import atexit

atexit.register(clean_up)

Nothing I tried would fire the clean_up routine when
the program was force killed.

In addition I should mention that I can't us the OS
to delete the temp files because a shelled process
creates them.  It may be possible to still do it but
my limited experience does not allow me to pursue it.

Anyway, I am happy with the outcome even though I have
not found a way to detect when the program is force
killed.  It is unlikely that would ever occur as long
as the program does not lock up.  It is up to me to
make sure that doesn't happen. :-)

Thanks again.

-- 
<Wildman> GNU/Linux user #557453
"Philosophy is common sense with big words."
  -James Madison

[toc] | [prev] | [next] | [standalone]


#103509

FromRandy Day <randy.day@sasktel.netx>
Date2016-02-25 11:13 -0600
Message-ID<MPG.3138f24c69f3f5a0989692@freenews.netfront.net>
In reply to#103478
In article <DumdnVLDRa4MEFPLnZ2dnUU7-fWdnZ2d@giganews.com>, 
best_lay@yahoo.com says...

[snip]

> Anyway, I am happy with the outcome even though I have
> not found a way to detect when the program is force
> killed.  It is unlikely that would ever occur as long

When your peogram starts, have it create
a small file. As part of your graceful
shutdown, delete the file.

Any subsequent processes can test for 
the existence of the file; if it exists, 
your program did not exit gracefully.

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web