I can’t close the Tkinter Window

Asked

Viewed 2,705 times

1

Well I am making a python application using Tkinter, I made a login window after checking user and password I would like to open a system window and close the login window. I’m breaking my head and I can’t find the error, if anyone can help I’ll be very grateful. I’ve researched everything I tried to last . quit . but does 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()
  • You tried to call the self.Janela.destroy() just before creating the new Tk instance (in the line before the principal = Tk()) and it didn’t work? Some error message appears?

  • incredibly does not give any error and also does not close.

  • Even putting the self.Janela.destroy() a line before the principal = Tk()? I tested it here in python 3.6 and it worked. Which python version do you use?

  • Thank you!! was that I put Destroy in the function of opening the system and it worked, I was putting in the function of user’s tool and password, but still do not understand the error why I can not close putting there

1 answer

0

To close the previous window, you can call the method destroy() of the instance self.Janela, however, it must be called before to create the new instance (principal = Tk()). The code of the function abreSistema gets 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 method sair:

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 method verificaSenhaClick.

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

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

  • Thanks! But there was still a doubt, for example if I put the function self.sair() only in the ways verificaSenha after the self.abreSistema() it should not close too ? Because in this case it does not close?

  • I added more information to the reply. The window probably won’t close because there are 2 instances of the Tk running

Browser other questions tagged

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