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


Groups > comp.lang.python > #103422

Re: [Newbie] Tkinter Question

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Kaynor <ckaynor@zindagigames.com>
Newsgroups comp.lang.python
Subject Re: [Newbie] Tkinter Question
Date Tue, 23 Feb 2016 14:41:07 -0800
Lines 50
Message-ID <mailman.81.1456267296.20994.python-list@python.org> (permalink)
References <Ye-dnZQLyOFiR1HLnZ2dnUU7-YPOydjZ@giganews.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de lfp/Kt6pZosvdveCJ8v5VgOH+8OOAvCrMFvp9boyMUrQ==
Return-Path <ckaynor@zindagigames.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.03; 'win32': 0.03; 'needed,': 0.05; 'subject:Question': 0.05; 'executed': 0.07; 'exit': 0.07; 'closed.': 0.09; 'exits': 0.09; 'handlers': 0.09; 'returns,': 0.09; 'email addr:python.org&gt;': 0.10; 'python': 0.10; 'files.': 0.13; 'properly': 0.15; '2016': 0.16; '23,': 0.16; 'api,': 0.16; 'atexit': 0.16; 'basic.': 0.16; 'email addr:python.org>': 0.16; 'fire,': 0.16; 'involved,': 0.16; 'linux).': 0.16; 'middle,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Tkinter': 0.16; 'temp': 0.16; 'tkinter.': 0.16; 'which,': 0.16; 'wrote:': 0.16; 'app': 0.16; 'deleted.': 0.18; 'creates': 0.18; 'gui': 0.18; 'subject:] ': 0.19; 'windows': 0.20; 'prevent': 0.20; 'to:name:python- list@python.org': 0.20; 'disable': 0.22; 'tkinter': 0.22; 'programming': 0.22; '(or': 0.23; 'feb': 0.23; "python's": 0.23; 'somewhere': 0.24; 'header:In-Reply-To:1': 0.24; 'command': 0.26; 'chris': 0.26; 'appreciated.': 0.27; 'external': 0.27; 'message- id:@mail.gmail.com': 0.27; 'behind.': 0.29; "i'm": 0.30; 'code': 0.30; 'window': 0.30; 'primary': 0.31; 'probably': 0.31; 'option': 0.31; 'another': 0.32; 'options': 0.33; 'url:python': 0.33; 'apply,': 0.33; 'displayed': 0.33; 'limitations': 0.33; 'similar': 0.33; 'maintaining': 0.34; 'tue,': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'closing': 0.36; 'url:library': 0.36; 'visual': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'being': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'button': 0.38; 'files': 0.38; 'rather': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'url:3': 0.60; 'care': 0.60; 'your': 0.60; 'close': 0.61; "you'll": 0.61; 'matter': 0.63; 'ending': 0.63; 'is.': 0.63; 'capture': 0.66; 'python-list': 0.66; 'email name:python-list': 0.67; 'talking': 0.67; 'power': 0.72; 'click': 0.76; 'temporary.': 0.84; 'aka': 0.91; 'serious': 0.97
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=zindagigames-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=J/1JSLy+dWj/udC6wQNeVgvpPJyLWwEn4xv0HjTi96g=; b=l9/57a4SyujYxr1/FianDJBeiw15v153Zb3fBEqRkF03ItW38m2qEnfnbyGMnMg7Uo 2WvgQVOKu1AvDRHXsq1JMgx7q9j6vTDNwQhbhSEcHDR8sWkdUhj1cxspzB+dNb5LO/OP k3qPZeq/GLqc5SaVMfi+43wXUrHYX2dxd6T6eOMMH7tmKB0+oDs0Kzx88rulrtN84Z8G aAxLYWXaJfomnVM4rWjRMmH/yxhNeUIo0tDr8JZrCTLfIuDIkUlPunJMGhAD/lr+WLR+ TGnw/F5PXuyXAS8bnoIEkGfDI3knT9itGe54tkQ0ZGa+h2hdZv/hm4Hi0feeOBdaNQJx A3zA==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=J/1JSLy+dWj/udC6wQNeVgvpPJyLWwEn4xv0HjTi96g=; b=BeHCYG9GywOPx8E/bsnBEswe6tV/ji0unvmxSFU0+WHIIV3afDZmoUghgMKP8wR41T 3hfU4irWUhqjrRgyui1ZzvRacmHHyJEqv3XRU6s8eNlCxrCQyCuKAOxrq5yeJfeKonnC BJbyURBo7+Yow5XT/sdJIZLP70hr423CWzACwTyAy1R3+e08eHfvu2/z9Ke/9XWVYwqI qE3UPBaDB0Y9qI8RVsJYAICuXeQOtrtpxWKWXpIcJAiA0l5dwAnDMZGuB38BOfbwEVNf cs5a1jKBlTHqS3/jhN1sO5VSGWIEbH+U1KbZ6165EGltWWgflqg8/mVaqQZTXp9I0K3v tEQw==
X-Gm-Message-State AG10YOSIsrtfvRI6xDn7gGK4mewDekv/tyZwrGZMNGrris5KV5YsYACOosooVijRz7G4Y5GpIMWxy69e9xPfdQ==
X-Received by 10.31.192.147 with SMTP id q141mr30910787vkf.96.1456267286752; Tue, 23 Feb 2016 14:41:26 -0800 (PST)
In-Reply-To <Ye-dnZQLyOFiR1HLnZ2dnUU7-YPOydjZ@giganews.com>
X-Content-Filtered-By Mailman/MimeDel 2.1.21rc2
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:103422

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[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

csiph-web