Tkinter question
Logan
logan at phreaker.nospam
Fri Nov 28 14:53:16 EST 2003
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.).
HTH, L.
--
mailto: logan at phreaker(NoSpam).net
More information about the Python-list
mailing list