1
class StartUp():
    def __init__(self):
        pass

    def verify(self):
        username = ("s")
        password = ("s")
        
        usernameEntry = usernameVar.get()
        passwordEntry = passVar.get()

        start = StartUp()

        if usernameEntry == username and passwordEntry == password:
            start.login() 
        else: 
            #messagebox.showerror("Error","Wrong Credentials")

            
    def login(self):
        #Create a window
        global usernameVar, passVar
        
        verify = StartUp()
        
        window = Tk()
        window.title("Login")
            
        userPassLabel = Label(window, font="Helvetica 18 bold", text="Royal Mail")
        userPassLabel.grid(row=0, column=0, sticky=W)

        usernameVar = StringVar()
        usernameLabel = Label(window, font="Arial", text="Username:")
        usernameLabel.grid(row=1, column=0, sticky=W)
        usernameEntry= Entry(window, width=30, bg="light blue",textvariable = usernameVar, )
        usernameEntry.grid(row=1, column=1, sticky=W)
            
        passVar = StringVar()
        passLabel = Label(window, font="Arial", text="Password:")
        passLabel.grid(row=2, column=0, sticky=W)
        passEntry= Entry(window, width=30, bg="light blue",textvariable = passVar, show ="●")
        passEntry.grid(row=2, column=1, sticky=W)
        
        b1= Button(window, text="Enter", command=verify.verify())
        b1.grid(row=3, column=0, sticky=W)

        window.mainloop()


start = StartUp()
start.login()

The code seems to not work. The message box will just pop up. The enter button then doesn't work. Not sure what is wrong. I'm very new to OOP so not sure whether it's to do with that.

An additional question is how am I able to carry one variable from an input box in one window/class to another window/class? Is global a viable option or is there a better way I can use .get() from an Entry box.

Thanks for the help

sebtheoo
  • 125
  • 10

1 Answers1

2

When you use (), you are calling the function immediately. And when you use () with a button, it destroys the purpose of having a button itself. So remove the parenthesis, ().

b1 = Button(window, text="Enter", command=verify.verify)

When the function gets called, the value inside the entry are empty and hence else is triggered.

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
  • This fixed it, thanks :) – sebtheoo Apr 04 '21 at 15:13
  • @CoolCloud: Re-answering duplicate questions is not the best way to boost your rep… – martineau Apr 04 '21 at 15:19
  • @CoolCloud yeah, you answered so quick I had to wait 5 mins :) – sebtheoo Apr 04 '21 at 15:21
  • @martineau There are a lot of duplicate questions getting re answered daily. I know its not an excuse. Finding existing _famous_ answers among other repeated and duplicated questions is not easy as it seems. Check [this](https://stackoverflow.com/search?tab=votes&q=function%20executed%20before%20clicking%20button%20tkinter&searchOn=3) – Delrius Euphoria Apr 04 '21 at 15:24
  • @sebtheoo I prefer you unmark my answer as correct one, so I can delete this, because this is a clear duplicate. – Delrius Euphoria Apr 04 '21 at 15:24
  • 1
    @martineau Most of the time it's easier to just answer the question. I think that that is a flaw with stackoverflow. The only solution that I can think of is a website with the most common tkinter mistakes that I can just bookmark. – TheLizzard Apr 04 '21 at 15:41
  • @TheLizzard: One of its many (flaws). I, too, have bookmarked many of the more common ones (actually they're in sub-folders classified by topic). This flaw especially bugs me because it means the laziness of the person answering (as well as the OP's because they didn't bother to look either) is being rewarded which just reinforces the behaviour. – martineau Apr 04 '21 at 15:51
  • 1
    @martineau Laziness? I mean we all have our own job apart from answering questions and looking for duplicates too right? I did have all this copied somewhere, but I lost all of it on a system reset too. We could just ignore this question and wait for someone else to mark as duplicate too though. But I don't think ignoring and hoping is the best practice. – Delrius Euphoria Apr 04 '21 at 15:55