Tkinter question
Cameron Laird
claird at lairds.com
Sun Nov 30 11:54:26 EST 2003
In article <pan.2003.11.28.19.53.15.767100 at phreaker.nospam>,
Logan <logan at phreaker.nospam> wrote:
>On Fri, 28 Nov 2003 19:59:23 +0100, Ali El Dada wrote:
>
>> i am using Tkinter in my application, and i have a button
>> that, when clicked, opens a new window as in:
>>
>> b1 = Button(someframe, text="bla", command = someFunction)
>>
>> def someFunction():
>> newWindow = Toplevel()
>> '''the new window widgets go here'''
>>
>> of course, whenever the button is clicked, a new window
>> opens. what do you recommend as a neat way to allow only one
>> window to open??
>
>It depends on what kind of behavior you want: if newWindow is e.g.
>a dialog (like 'find', 'find & replace' etc. in an editor), you
>want the new window to pop up, but any older dialog should get
>destroyed.
>
>To achieve this, you can make newWindow a *global variable* and
>use e.g. the following (not very elegant, but it works):
>
># here, the 'find'-dialog is created (e.g. inside some class)
> try:
> newWindow.destroy()
> except:
> pass
>
> newWindow = Toplevel()
> # widgets for the 'find'-dialog
>
># here, the 'find&replace'-dialog is created (e.g. inside some class)
> try:
> newWindow.destroy()
> except:
> pass
>
> newWindow = Toplevel()
> # widgets for the 'find&replace'-dialog
>
>
>If you want, that your window gets created only once (and whenever
>such a window is already open, no new window should be created) you
>could either use an approach similar to the one above (i.e. with
>newWindow being a global variable) or e.g. use a class which
>keeps track on how many instances of itself were already created
>and which behaves accordingly (Google: python, singleton).
>
>There are other solutions, too. The 'right' solution for you
>depends mainly on the design of your whole program (OO or not etc.).
.
.
.
It's quite common with some toolkits--Tkinter
among them--to re-use widgets. The body of a
program just uses widgets as needed, and
little initializers or constructors or helpers
take care of creating (or sometimes deiconify-
ing) the widgets as needed. They aren't
destroyed, in general, although they might oc-
casionally be iconified or withdrawn or
unmanaged or such.
--
Cameron Laird <claird at phaseit.net>
Business: http://www.Phaseit.net
More information about the Python-list
mailing list