Python tkinter: Window works perfectly, but does not close after executing a function

0

code:

def editar (self, event=None):
    ob = self.buffer(opcao=2)
    if (ob[1] == None):
        return
    elif (ob[0] == None):
        return

    root = Tk()
    root.withdraw()

    for o in self.lista:
        if (ob[0].codigo.upper() == o.codigo):
            #solicita a certeza do usuário quanto à edição
            v = askyesnocancel('Primeira rodada',
                               message='Voce tem certeza?\n'+
                               str(ob[1].semestre) + 
                               '\n'+ob[1].nome_completo.upper() + 
                               '\n'+ob[1].requisitos.upper() +
                               '\n'+str(ob[1].horas) +
                               '\n'+ob[1].estado_atual.upper() +
                               '.')
            if (v):
                dic = Disciplina(ob[1].semestre,
                                 o.codigo,
                                 ob[1].nome_completo.upper(),
                                 ob[1].horas,
                                 ob[1].requisitos.upper(),
                                 ob[1].estado_atual.upper())
                ind = lista.index(o)
                self.lista.pop(ind)
                self.lista.insert(ind, dic)

            break

This code is responsible for editing a Discipline object, in which it simply removes the object from the list and inserts a similar object in the same position.

Everything here works perfectly well. But the window that details the object and contains the button it calls by this function remains open. Since it should call this function and close the window to return to the initial window, which is a list of all objects.

The function calling by the above function:

def detalha (self, event=None, opcao='Editar'):
    self.janela.iconify() #minimiza a janela principal

    if (not 'adiciona' in opcao.lower()):
        item = self.listar.get(self.listar.curselection()[0])
        t = ler.achaMat(item.split(' ')[1][:6], self.lista)[1]
        det = Det(t.semestre, t.codigo, t.nome_completo,
                  t.horas, t.requisitos, t.estado_atual, opcao)
        det.janela.wm_protocol("WM_TAKE_FOCUS", det.imprime)
    else:
        det = Det(tp=opcao) #tp é o que será mostrado no botao


    if ('edita' in opcao.lower()):
        det.botao.bind("<ButtonRelease-1>", self.editar)
    elif ('exclui' in opcao.lower()):
        det.botao.bind("<ButtonRelease-1>", self.excluir)
    elif ('adiciona' in opcao.lower()):
        det.botao.bind("<ButtonRelease-1>", self.adiciona)

    #Mostra a janela principal e reseta a lista 
    det.janela.bind("<Destroy>", self.reseta)

Before asking, the button calls the action (self, event = None) and at the end of it has the call to another function self.fecha () that has as code:

   def fecha (self, event=None):
       self.janela.destroy()
    
asked by anonymous 13.10.2016 / 18:59

1 answer

1

I solved it partially by simply placing the window that should close as a parameter. So I play root directly and do not need anything extra. Now it's another.

def editar (self, event=None, jan = None):
    ob = self.buffer(opcao=2)
    if (ob[1] == None):
        return
    elif (ob[0] == None):
        return
    print (ob[0].__dict__,"\n",
           ob[1].__dict__,"\n")

    if (jan != None):
        root = jan
    else:
        root = Tk()
        root.withdraw()

    mensagem = ('Voce tem certeza?\n\nModificacoes:'+
                str(ob[0].semestre) + "=>" + str(ob[1].semestre) + "\n"+
                ob[0].nome_completo + "=>" + ob[1].nome_completo + "\n"+
                ob[0].requisitos + "=>" + ob[1].requisitos + "\n"+
                str(ob[0].horas) + "=>" + str(ob[1].horas) + "\n"+
                ob[0].estado_atual+ "=>" + ob[1].estado_atual
                )

    v = askyesnocancel('Primeira rodada',
                           message=mensagem)

    dic = Disciplina(ob[1].semestre,
                     o.codigo,
                     ob[1].nome_completo.upper(),
                     ob[1].horas,
                     ob[1].requisitos.upper(),
                     ob[1].estado_atual.upper())

    if (v):
        ind = self.lista.index(ob[0])
        self.lista.pop(ind)
        self.lista.insert(ind, dic)

    root.cancela()

I think the problem had to do with the need to put a root window

    root = TK()
    root.withdraw()

To disappear with the ghost window at the time of the askyesnocancel that I needed.

    
14.10.2016 / 18:06