3

My son is trying to learn python and wondered if someone could help on here?

The issue we are having is that we can not get password validation to work. Nothing fancy, 1 st window - enter name and password

if correct - 2nd window pops up

if incorrect - 3 window pops up

We have the following code, however the third window also pops up.

I suspect it's something to do with def main or variable.

from tkinter import*

window1=Tk()
window1.title("First window")

username="user"
password="password"

def main():
    if eone == "user" and etwo == "password":
        master=tk()
        master.title("second window")
    else:
        master1=tk()
        master1.title("third window")

Label(window1, text="username").grid(row=0)
Label(window1, text="password").grid(row=1)

eone=Entry(window1)
etwo=Entry(window1, show='*')

eone.grid(row=0, column=1)
etwo.grid(row=1, column=1)

b1 = Button(window1, text="login") ,bg='00c714',fg='#ffffff',command=main())
b1.grid(row=3, column=0, sticky=E)

b2=Button(window1, command=window1.destroy, text="exit", bg='#fc0303' ,fg='#ffffff')
b2.grid(row=3, column=1, sticky=E)

mainloop()

He has spent many hours on it yesterday and would appreciate any help Thanks

3
  • you should use Tk() only to create main window. For other windows you should use Toplevel() Commented Feb 18, 2021 at 20:42
  • command= expects function's name (so called "callback")- it means without () - command=main Commented Feb 18, 2021 at 20:43
  • import * is not preferred. See more: PEP 8 -- Style Guide for Python Code Commented Feb 18, 2021 at 20:44

1 Answer 1

3

First, the code you posted gave some errors so fixing them:

  • replacing tk() with ~~Tk()~~ Toplevel() (see the comments)
  • replacing '00c714' with '#00c714'
  • removing parantheses here "login")

now it becomes "compile" time error-free. As for your question, 2 things need changing:

  1. When we need to give a callback / command to a button, we give the function itself and not call it! IOW, command=main() will lead to calling main right away when program runs (without pressing to button). Instead, we should do command=main (note the lack of parantheses). Actually this is what is done here also: command=window1.destroy - we need to give the function itself and tkinter will call it with parantheses when button is pressed.

  2. eone == "user" This compares the tkinter Entry widget directly with the string "user"! What you meant is through get method of entries: eone.get() == "user". Same goes for etwo too.

Overall, here is the code with these modifications (and some PEP-8 compliant formatting):

from tkinter import*

window1 = Tk()
window1.title("First window")

username = "user"
password = "password"

def main():
    # Change here: using `get` to get what is written in entries
    if eone.get() == "user" and etwo.get() == "password":
        master = Toplevel()
        master.title("second window")
    else:
        master1 = Toplevel()
        master1.title("third window")

Label(window1, text="username").grid(row=0)
Label(window1, text="password").grid(row=1)

eone = Entry(window1)
etwo = Entry(window1, show='*')

eone.grid(row=0, column=1)
etwo.grid(row=1, column=1)

# Change here: using function `main` itself instead of calling it
b1 = Button(window1, text="login", bg="#00c714", fg="#ffffff", command=main)
b1.grid(row=3, column=0, sticky=E)

b2 = Button(window1, command=window1.destroy, text="exit", bg="#fc0303", fg='#ffffff')
b2.grid(row=3, column=1, sticky=E)

mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

There's still one major problem with the code: multiple calls to Tk(). It's not going to exhibit a problem with the code so far, but it basically breaks Tkinter because you've created multiple independent instances of it. You must use Toplevel() instead to create additional windows.

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.