I can not close the Tkinter Window

1

Well I'm doing an application in python using tkinter, I made a login window after the user verification and password I would like to open a system window and close the login window. I'm breaking my head and I can not find the error, if anyone can help I'll be very grateful. I already researched everything I tried to use .quit .destroy but did not close.

from tkinter import *
from sistemas import Sistema

class loginJanela:

    def __init__(self):

        self.Janela = Tk()
        self.Janela.title('nome')
        self.Janela.iconbitmap('icone.ico')
        w = self.Janela.winfo_screenwidth()
        h = self.Janela.winfo_screenheight()
        size = tuple(int(_) for _ in self.Janela.geometry().split('+')[0].split('x'))
        x = w/2 - size[0]/2
        y = h/2 - size[1]/2
        self.Janela.geometry("280x280+%d+%d" % (x,y))

        self.fontePadrao = ('Arial', "10")
        self.container1 = Frame(self.Janela)
        self.container1['pady'] = 20
        self.container1.pack()

        self.containerDados1 = Frame(self.Janela)
        self.containerDados1['padx'] = 50
        self.containerDados1.pack()

        self.containerDados2 = Frame(self.Janela)
        self.containerDados2['padx'] = 50
        self.containerDados2.pack()

        self.containerBotao = Frame(self.Janela)
        self.containerBotao['pady'] = 30
        self.containerBotao.pack()

        self.titulo = Label(self.container1, text='LOGIN')
        self.titulo['font'] = ('Arial', '10', 'bold')
        self.titulo.pack()

        self.loginId = Label(self.containerDados1, text='ID:')
        self.loginId['font'] = self.fontePadrao
        self.loginId.pack()

        self.usuario = Entry(self.containerDados1)
        self.usuario["width"] = 30
        self.usuario.pack()

        self.loginSenha = Label(self.containerDados2, text='Senha:')
        self.loginSenha['font'] = self.fontePadrao
        self.loginSenha.pack()

        self.senha = Entry(self.containerDados2, show='*')
        self.senha.bind('<Return>', self.verificaSenhaEnter)
        self.senha["width"] = 30
        self.senha.pack()

        self.botao = Button(self.containerBotao, text='Entrar')
        self.botao['font'] = ('Calibri', '10')
        self.botao['width'] = 12
        self.botao['command'] = self.verificaSenhaClick
        self.botao.pack()
        self.mensagem = Label(self.containerBotao, text='', font=self.fontePadrao)
        self.mensagem.pack()

        self.Janela.mainloop()

    def sair(self):
        self.Janela.quit()

    def abreSistema(self):
        self.container1.pack_forget()
        self.containerDados1.pack_forget()
        self.containerDados2.pack_forget()

        principal = Tk()
        principal.title('IMOBILIÁRIA SK')
        principal.iconbitmap('icone.ico')
        principal.geometry("800x600")

        Sistema(principal)
        principal.mainloop()


    def verificaSenhaClick(self):
        usuario = self.usuario.get()
        senha = self.senha.get()
        if usuario == '' and senha == '':
            self.mensagem['text'] = 'Autenticado'
            self.abreSistema()
            self.sair()

        else:
            self.mensagem['text'] = 'Erro na autenticação'



    def verificaSenhaEnter(self, event):
        usuario = self.usuario.get()
        senha = self.senha.get()
        if usuario == 'skimoveis' and senha == 'senhasecreta':
            self.mensagem['text'] = 'Autenticado'
            self.abreSistema()
            self.sair()

        else:
            self.mensagem['text'] = 'Erro na autenticação'





lp = loginJanela()
    
asked by anonymous 11.04.2018 / 00:29

1 answer

0

To close the previous window, you can call the destroy() method of the instance self.Janela , however, it must be called before to create the new instance ( principal = Tk() ). The code for the abreSistema function looks like this:

def abreSistema(self):
    self.container1.pack_forget()
    self.containerDados1.pack_forget()
    self.containerDados2.pack_forget()

    self.Janela.destroy() # <= Aqui, você "destrói" a instância self.Janela
    principal = Tk()
    principal.title('IMOBILIÁRIA SK')
    principal.iconbitmap('icone.ico')
    principal.geometry("800x600")

    Sistema(principal)
    principal.mainloop()

If you prefer to use the sair method:

def sair(self):
    self.Janela.destroy()

def abreSistema(self):
    self.container1.pack_forget()
    self.containerDados1.pack_forget()
    self.containerDados2.pack_forget()

    self.sair()
    principal = Tk()
    principal.title('IMOBILIÁRIA SK')
    principal.iconbitmap('icone.ico')
    principal.geometry("800x600")

    Sistema(principal)
    principal.mainloop()

And there's no need to call sair within the verificaSenhaClick method.

It is not recommended to use more than one instance of Tk in the same application, and possibly the problem occurred because the second instance was created before the first instance was destroyed.

Alternatively, you can modify the application to create just one root instance, and open the new window (s) with the command: Toplevel (documentation in English: python and tkdocs )

    
11.04.2018 / 02:47