KhalaMan2000
KhalaMan2000

Reputation: 11

Calling function not working for me in a class

I am trying to make a button using tkinter, which when pressed, it should delete all buttons on the screen, then it should make them reappear when I call the function for the original buttons. This is my intention but I cannot get it to work.

class login_screen:

    def __init__(self, master):
        self.master = master

        def menu_buttons():
            self.login_button = Button(master,
                              text="Login",
                              command=self.login,
                              relief=RIDGE,
                              height=2,
                              width=7,
                              bg="#ffb380")

            self.register_button = Button(master,
                              text="Register",
                              command=self.register,
                              relief=RIDGE,
                              height=2,
                              width=7,
                              bg="#ffb380")

            self.login_button.place(x=95, y=90)
            self.register_button.place(x=95, y=155)

        menu_buttons()


    def clear(self):
        self.login_button.destroy()
        self.register_button.destroy()

    def login(self):
        self.clear()
        menu_buttons()

Error:

NameError: name 'menu_buttons' is not defined

When I press the login_button, it should clear everything then make the same buttons appear again. At the moment it only clears but does not reappear.

Upvotes: 0

Views: 71

Answers (3)

ShayneLoyd
ShayneLoyd

Reputation: 633

There is no need to completely destroy the buttons, so there is no need to attempt to recreate them in init since they still exists but are not visible.

class login_screen:

    def __init__(self, master):
        self.master = master
        self.login_button = Button(master,
                                   text="Login",
                                   command=self.login,
                                   relief=RIDGE,
                                   height=2,
                                   width=7,
                                   bg="#ffb380")

        self.register_button = Button(master,
                                      text="Register",
                                      command=self.register,
                                      relief=RIDGE,
                                      height=2,
                                      width=7,
                                      bg="#ffb380")

    def place_buttons()
        self.login_button.place(x=95, y=90)
        self.register_button.place(x=95, y=155)

    def clear(self):
        self.login_button.place_forget()
        self.register_button.place_forget()

    def login(self):
        self.clear()
        self.place_buttons()

Upvotes: 0

C.Nivs
C.Nivs

Reputation: 13106

If you are trying to re-use menu_buttons, don't define it in __init__, because the function will disappear as soon as __init__ finishes, and so you won't be able to call it again:

class login_screen:

    def __init__(self, master):
        self.master = master
        self.menu_buttons()


    # define the function here, so it can re-run
    def menu_buttons(self):
        self.login_button = Button(self.master,
                          text="Login",
                          command=self.login,
                          relief=RIDGE,
                          height=2,
                          width=7,
                          bg="#ffb380")

        self.register_button = Button(self.master,
                          text="Register",
                          command=self.register,
                          relief=RIDGE,
                          height=2,
                          width=7,
                          bg="#ffb380")

        self.login_button.place(x=95, y=90)
        self.register_button.place(x=95, y=155)


    def clear(self):
        self.login_button.destroy()
        self.register_button.destroy()

    def login(self):
        self.clear()
        # call self.menu_buttons to re-initialize
        self.menu_buttons()



Upvotes: 0

gpopides
gpopides

Reputation: 736

you should take of init the menu_buttons method and then pass self argument to menu_buttons

def menu_buttons(self):

At the moment menu_buttons is accesible only by your init method and other methods can't access it.

Upvotes: 1

Related Questions