Tkinter question

A

Ali El Dada

hi all:

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?? is it a good idea to use classes and making
the new window a member of the first? will it solve the
problem?

thanks...
 
L

Logan

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.
 
C

Cameron Laird

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top