Search code examples
pythontkinter

Why isn't the tkinter frame being hidden?


When the username is hello and the password is world, why isn't the frame being hidden (forgotten)

I've tried to .destroy() which only removes the button

Heres my code:


import tkinter as tk
from tkinter import colorchooser, messagebox


class ExpenseTracker(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Expense Tracker")
        self.geometry("700x700")
        self.minsize(700, 700)
        self.login = Login(self)
        self.mainloop()


class Login(tk.Frame):
    def __init__(self, parent):
        self.username = tk.StringVar()
        self.password = tk.StringVar()
        super().__init__(parent)
        tk.Label(text="Enter Username:", borderwidth=0).place(relx=0.5, rely=0.36, anchor=tk.CENTER)
        tk.Label(text="Enter Password:", borderwidth=0).place(relx=0.5, rely=0.46, anchor=tk.CENTER)
        self.change_colour = tk.Button(text="Background Colour", command=self.change_colour)
        self.change_colour.place(relx=0.9, rely=0.1, anchor=tk.CENTER)
        self.exit_button = tk.Button(text="Exit", command=exit)
        self.exit_button.place(relx=0.9, rely=0.05, anchor=tk.CENTER)
        self.username_entry = tk.Entry(textvariable=self.username)
        self.username_entry.place(relx=0.5, rely=0.4, anchor=tk.CENTER)
        self.password_entry = tk.Entry(textvariable=self.password)
        self.password_entry.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
        self.login_box = tk.Button(self, text="Login", command=self.get_details)
        self.login_box.place(relx=0.5, rely=0.55, anchor=tk.CENTER)
        self.pack(expand=True, fill=tk.BOTH)

    def change_colour(self):
        color = colorchooser.askcolor()[1]
        self.configure(bg=color)

    def get_details(self):
        __username = self.username.get()
        __password = self.password.get()
        if __username == "hello" and __password == "world":
            self.grid_forget()


ExpenseTracker()

could anyone please help, it seems the function is referring to only the button, not the entire frame?


Solution

  • Firstly u need to pass self as parent all components. And you need to change self.grid_forget to self.pack_forget(). Because u using pack for packing your components.

    Here is your revised code.

    import tkinter as tk
    from tkinter import colorchooser, messagebox
    
    
    class ExpenseTracker(tk.Tk):
        def __init__(self):
            super().__init__()
            self.title("Expense Tracker")
            self.geometry("700x700")
            self.minsize(700, 700)
            self.login = Login(self)
            self.mainloop()
    
    
    class Login(tk.Frame):
        def __init__(self, parent):
            self.username = tk.StringVar()
            self.password = tk.StringVar()
            super().__init__(parent)
            tk.Label(self,text="Enter Username:", borderwidth=0).place(relx=0.5, rely=0.36, anchor=tk.CENTER)
            tk.Label(self,text="Enter Password:", borderwidth=0).place(relx=0.5, rely=0.46, anchor=tk.CENTER)
            self.change_colour = tk.Button(self,text="Background Colour", command=self.change_colour)
            self.change_colour.place(relx=0.9, rely=0.1, anchor=tk.CENTER)
            self.exit_button = tk.Button(self,text="Exit", command=exit)
            self.exit_button.place(relx=0.9, rely=0.05, anchor=tk.CENTER)
            self.username_entry = tk.Entry(self,textvariable=self.username)
            self.username_entry.place(relx=0.5, rely=0.4, anchor=tk.CENTER)
            self.password_entry = tk.Entry(self,textvariable=self.password)
            self.password_entry.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
            self.login_box = tk.Button(self, text="Login", command=self.get_details)
            self.login_box.place(relx=0.5, rely=0.55, anchor=tk.CENTER)
            self.pack(expand=True, fill=tk.BOTH)
    
        def change_colour(self):
            color = colorchooser.askcolor()[1]
            self.configure(bg=color)
    
        def get_details(self):
            __username = self.username.get()
            __password = self.password.get()
            if __username == "hello" and __password == "world":
                self.pack_forget()
    
    
    ExpenseTracker()