Python Tkinter: Window works perfectly but does not close after performing a function

Asked

Viewed 1,296 times

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 that calls 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 that calls by the function above:

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 you ask, the knob calls the function acao(self, Event=None) and at the end of it has the call to another function self.fecha() which has as code:

   def fecha (self, event=None):
       self.janela.destroy()
  • Have you tried calling self.fecha() before the return (if you have) in function self.editar?

  • In these functions I don’t call any Return. Return is only for error. They don’t do anything else. I tried to do anything to interact information between windows (there are 4: main, search, route and detail), but the easiest way was to save to any file (buffer.txt) and load soon after by any other function

1 answer

1


I resolved partially, simply by placing the window that should close as parameter. So I play straight in root 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 get rid of the ghost window at the time of the askyesnocancel that I needed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.