0

I created a code using just functions. I am now putting the functions into classes.

I created the function:

    def login()
        login_button = Button(root, text="Click here to enter data: ",
                              command=lambda: validation(username_input_box.get(), password_input_box.get()))

        login_button.place(x=210, y=240)

        quit_button = Button(root, text="Quit", command=root.quit)
        quit_button.place(x=265, y=300)

I have now put it in a class called "setup" (the class has other function):

class setup:
    def login(self):
        login_button = Button(root, text="Click here to enter data: ",
                              command=lambda: self.validation(username_input_box.get(), password_input_box.get()))

        login_button.place(x=210, y=240)

        quit_button = Button(root, text="Quit", command=root.quit)
        quit_button.place(x=265, y=300)

I want to call the function login() from outside the class. I know normally you would just do login(), but It says "login" is not defined. Any idea on how to do this?

thanks x

2
  • Why do you think it should be in a class? Commented Jan 19, 2021 at 17:51
  • that's only part of the code x Commented Jan 19, 2021 at 18:18

2 Answers 2

1

To access the method of a class, we need to instantiate a class into an object. Then we can access the method as an instance method of the class as shown in the program below.

class setup:
    def login(self):
        login_button = Button(root, text="Click here to enter data: ",
                              command=lambda: self.validation(username_input_box.get(), password_input_box.get()))

        login_button.place(x=210, y=240)

        quit_button = Button(root, text="Quit", command=root.quit)
        quit_button.place(x=265, y=300)

obj = setup()
obj.login()
Sign up to request clarification or add additional context in comments.

Comments

0

You could use a static method decorator.

For example:

class setup:
    @staticmethod
    def login():
        login_button = Button(root, text="Click here to enter data: ",
                              command=lambda: self.validation(username_input_box.get(), password_input_box.get()))

        login_button.place(x=210, y=240)

        quit_button = Button(root, text="Quit", command=root.quit)
        quit_button.place(x=265, y=300)

Note that I've removed the self parameter, since login wasn't using local variables. If it needs to, you may need to do this slightly differently.

Now you can call the function from outside the class without instantiating an object, like so:

setup.login()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.