-1

Whenever I try running a function after pressing a button, rather than run the function, I get an error saying

Button(text = "Register", height = "2", width = "30", command = lambda : self.RegScreen()).pack() AttributeError: 'str' object has no attribute 'RegScreen'

I'm not sure what's causing this error or how I am meant to solve this. Below is the code I have used to far. Any help is greatly appreciated.

class Main:
    def __init__(self, screen):
        self.screen = Tk()
        self.loginscreen = StringVar()
        self.username = StringVar()
        self.password = StringVar()
        self.var_FName = StringVar()
        self.var_SName = StringVar()
        self.var_password1 = StringVar()
        self.var_usertype = StringVar()
        self.UserID = StringVar()


    def main_screen(self):
        #Main.addtohomeworkdb()
        screen = Tk()
        screen.geometry("300x250")
        screen.title("Main Menu")
        Label(text="Welcome to MyComputerScience!", bg="white", width="300", height="2", font=("Calibri", 13)).pack()
        Label(text="").pack()
        Button(text="Login", height="2", width="30", command=lambda: self.login).pack()
        Label(text="").pack()
        Button(text="Register", height="2", width="30", command=lambda: self.RegScreen).pack()
        Label(text="").pack()
        screen.mainloop()

    def RegScreen(self):
        print("bRuh")

if __name__ == '__main__':  
    Main.main_screen(self)
  • My best guess is that the parenthesis in `command=lambda: self.RegScreen()` are causing trouble. Try without them, like `command=lambda: self.RegScreen` or even better `command=self.RegScreen`. – accdias Dec 28 '19 at 12:36
  • Even without the parenthesis it still doesn't work. – MyNameJeff Dec 28 '19 at 12:39
  • 1
    What about removing `lambda:` as well? – accdias Dec 28 '19 at 12:40
  • 1
    Try: `Button(text="Register", height="2", width="30", command=self.RegScreen).pack()` as @accdias suggested – urban Dec 28 '19 at 12:43
  • Without lambda the screen doesn't load and it attempts to run self.regscreen automatically. – MyNameJeff Dec 28 '19 at 12:50
  • If there are no parameters to pass to the command function, there is no need for `lambda`. – progmatico Dec 28 '19 at 13:13
  • ***`AttributeError: 'str' object has no attribute 'RegScreen'`***: Where do you get this `self` in `Main.main_screen(self)` from? Also read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged). Take the tour [Python - Object Oriented](https://www.tutorialspoint.com/python/python_classes_objects.htm) – stovfl Dec 28 '19 at 14:23

1 Answers1

0

You are calling main_screen incorrectly. It is a method on an object, so you must first create an object.

You need to start your program like this:

main = Main()
main.main_screen()

You also have the problem that you're creating two root windows: one in __init__ and one in main_screen. You should only ever create a single instance.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685