tkinter destroy()

max(01)* max2 at fisso.casa
Tue Mar 29 17:33:04 CEST 2005


Eric Brunel wrote:
> On Tue, 29 Mar 2005 10:37:10 GMT, max(01)* <max2 at fisso.casa> wrote:
> 
>> hi people.
>>
>> when i create a widget, such as a toplevel window, and then i destroy
>> it, how can i test that it has been destroyed? the problem is that even
>> after it has been destroyed, the instance still exists and has a tkinter
>> name, so testing for None is not feasible:
>>
>>  >>> import Tkinter
>>  >>> fin = None
>>  >>> fin1 = Tkinter.Toplevel()
>>  >>> fin1.destroy()
>>  >>> print fin1
>> .1075951116
> 
> 
> The winfo_exists method is what you want:
> 
>>>> print fin1.winfo_exists()
> 
> 0
> 
> However, I'm curious about *why* you want to do that: since *you* made 
> the call to destroy, what would you want to do anything with a widget 
> you've already destroyed?

my main window has a button that opens another toplevel window. this 
button is bound to a method that creates the window and does some other 
processing *only if* the windows does not exist yet (otherwise, the user 
could click more on the button and multiple copies of the window would 
pop up, which is not what i want... in fact what i do is to assign None 
to a variable called self.dialogo in my main application code, then i do 
self.dialogo = Toplevel() inside the method...)

in this precise moment, though, it comes to my mind that i could achieve 
the purpose simply activating/deactivating the button according to a flag...

anyway, i attach the code, so you can see better what i mean...

anyone who has to say anything about is welcome of course...

it is a tentative adapration of a program found in the tcl/tk package demo

---cut here---

from Tkinter import *

class MiaApp:
   def __init__(self, genitore):

     self.mioGenitore = genitore
     self.fonte = ("Helvetica", 12)
     self.fonteVar = ("Helvetica", 14)

     self.quadro_grande = Frame(genitore)
     self.quadro_grande.pack(expand = YES, fill = BOTH)

     self.msg = Label(self.quadro_grande)
     self.msg.configure(
       font = self.fonte,
       wraplength = "10c",
       justify = LEFT,
       text = u"Sono qui sotto presentati tre pulsanti a spunta. \
Premendo un pulsante, se ne varia lo stato di selezione e si \
imposta una variabile a un valore che indica lo stato del \
pulsante stesso. Premendo il pulsante \u00ABMostra \
Variabili\u00BB si possono vedere i valori correnti delle \
variabili."
       )
     self.msg.pack(side = TOP)

     self.pulsanti = Frame(self.quadro_grande)
     self.pulsanti.pack(side = BOTTOM, fill = X, padx = "2m")

     self.pulsanti_spunta = Frame(self.quadro_grande)
     self.pulsanti_spunta.pack(side = TOP, fill = X, padx = "2m")

     self.annulla = Button(self.pulsanti)
     self.annulla.configure(
       text = "Annulla",
       command = self.mioGenitore.destroy
       )
     self.annulla.pack(side = LEFT, expand = YES)


     self.var = Button(self.pulsanti)
     self.var.configure(
       text = "Mostra Variabili",
       command = self.pulsanteMostraVariabiliPremuto
       )
     self.var.pack(side = LEFT, expand = YES)

     self.tergicristalli = IntVar()
     self.b1 = Checkbutton(self.pulsanti_spunta)
     self.b1.configure(
       text = "Tergicristalli a posto",
       variable = self.tergicristalli,
       relief = FLAT
       )
     self.b1.pack(
       side = TOP,
       pady = 2,
       anchor = W
       )

     self.freni = IntVar()
     self.b2 = Checkbutton(self.pulsanti_spunta)
     self.b2.configure(
       text = "Freni a posto",
       variable = self.freni,
       relief = FLAT
       )
     self.b2.pack(
       side = TOP,
       pady = 2,
       anchor = W
       )

     self.autista = IntVar()
     self.b3 = Checkbutton(self.pulsanti_spunta)
     self.b3.configure(
       text = "Autista sobrio",
       variable = self.autista,
       relief = FLAT
       )
     self.b3.pack(
       side = TOP,
       pady = 2,
       anchor = W
       )

     self.dialogo = None

   def mostraVariabili(self, *argomenti):
     self.dialogo = Toplevel()
     self.dialogo.wm_title("Valori delle variabili")

     self.dialogo.quadro_grande = Frame(self.dialogo)
     self.dialogo.quadro_grande.pack(expand = YES, fill = BOTH)

     self.dialogo.titolo = Label(self.dialogo.quadro_grande)
     self.dialogo.titolo.configure(
       text = "Valori delle variabili:",
       width = 20,
       font = self.fonteVar
       )
     self.dialogo.titolo.pack(side = TOP, fill = X)

     lung = 1
     for i in argomenti:
       if len(i) > lung:
         lung = len(i)

     self.dialogo.dq = {}
     self.dialogo.dn = {}
     self.dialogo.dv = {}
     for i in argomenti:
       self.dialogo.dq[i] = Frame(self.dialogo.quadro_grande)
       self.dialogo.dq[i].pack(
         side = TOP,
         anchor = W,
         fill = X
         )

       self.dialogo.dn[i] = Label(self.dialogo.dq[i])
       self.dialogo.dn[i].configure(
         text = i + ": ",
         width = lung + 2,
         anchor = W
         )
       self.dialogo.dn[i].pack(
         side = LEFT
         )

       self.dialogo.dv[i] = Label(self.dialogo.dq[i])
       self.dialogo.dv[i].configure(
         textvariable = self.freni,  ### FIXME
         anchor = W
         )
       self.dialogo.dv[i].pack(
         side = LEFT,
         expand = YES,
         fill = X
         )

     self.dialogo.vaBene = Button(self.dialogo.quadro_grande)
     self.dialogo.vaBene.configure(
       text = "Va Bene",
       command = self.pulsanteVaBenePremuto,
       default = ACTIVE
       )
     self.dialogo.vaBene.bind(
       "<Return>",
       self.pulsanteVaBenePremuto_a
       )
     self.dialogo.vaBene.focus_force()
     self.dialogo.vaBene.pack(
       side = BOTTOM,
       pady = 2
       )

   def pulsanteVaBenePremuto(self):
     self.dialogo.destroy()
     self.dialogo = None

   def pulsanteVaBenePremuto_a(self, evento):
     self.pulsanteVaBenePremuto()

   def pulsanteMostraVariabiliPremuto(self):
     if not self.dialogo:
       self.mostraVariabili("self.tergicristalli",
                            "self.freni",
                            "self.autista")  ### FIXME
     else:
       self.dialogo.lift(self.mioGenitore)

radice = Tk()
radice.wm_title("Dimostrazione Pulsanti a Spunta")
radice.wm_iconname("spunta")
miaApp = MiaApp(radice)
radice.mainloop()



More information about the Python-list mailing list