How to call a new frame after checking an input box entry in Tkinter?

Asked

Viewed 11 times

1

Good morning! I’m making a game where the user needs to enter the passwords he discovers in the course of the game to unlock new screens. However, I’m not getting my password-checker button to work properly. It calls a function to check the password and but I can’t get this function to change frame. I’m going to leave a portion of my code below and if anyone can help me, I’d be very grateful. This would be my main file, where the Sampleapp class is responsible for switching frames:

class SampleApp(tk.Tk):
    '''Essa classe vai iniciar o primeiro frame e, posteriormente, 
        ir chamando os outros frames
    '''
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        # Chama o frame inicial
        self.switch_frame(Comecar)

    def switch_frame(self, frame_class):
        '''Essa chasse será chamada quando algum botão for pressionado.
            Ela irá 'apagar' o frame atual e chamar o novo.
        '''
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.place(x= 0, y=0)


class Comecar(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.label = tk.Label(self, width = 1367, height = 703).pack(fill='both', expand=True)
        self.canvas1 = tk.Canvas(self, bg = "black", width = 1367, height = 703)
        self.canvas1.place(x= 0, y= 0)

        # Botão play
        self.botao_play = tk.Button(self, width = 20, height = 2, 
                            command = lambda: master.switch_frame(senha. Senha), anchor = tk.W)
        self.botao_play.configure(background= "red", activebackground = "red", relief = tk.FLAT,
                                        bd=0, highlightthickness=0)
        self.botao_play_window = self.canvas1.create_window(135, 262, anchor=tk.NW, window=self.botao_play)



if __name__ == "__main__":
    # Chama a classe que iniciará os frames.
    app = SampleApp()
    # Dá titúlo para a janela.
    app.title("teste")
    # Define o tamanho da janela
    app.geometry("1367x703")
    # Coloca tudo para rodar
    app.mainloop()

This would be the file that has the input box and the button that checks the password:

class Senha(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.label = tk.Label(self, width = 1367, height = 703).pack(fill='both', expand=True)

        self.canvas1 = tk.Canvas(self, bg = "blue", bd=0, highlightthickness=0, relief='ridge',
                                            width = 288, height = 512)
        self.canvas1.place(x= 542, y=98)
        
        # Caixa de input
        self.entry1 = tk.Entry (self, font=('Helvetica'), background = "white",
                                border=0, highlightthickness=0)
        self.canvas1.create_window(143, 264, window=self.entry1, width = 230, height = 20)        
        
        # Botão conferidor de senha
        self.button2 = tk.Button(self, text='CONFERIR',font=('Helvetica'), command = self.verificar, 
                                activebackground = "#a68d79", bg='#a68d79', fg='white',
                                relief = tk.FLAT, width = 10, bd=0, highlightthickness=0)
        self.canvas1.create_window(145, 300, window=self.button2)
        
    def verificar(self):
        self.x = self.entry1.get()
        if self.x == "oi":
            print("oi")

The command I use on my other buttons to change the frames is:

command = lambda: master.switch_frame(senha. Senha)
No answers

Browser other questions tagged

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