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
First, the code you posted gave some errors so fixing them:
tk() with ~~Tk()~~ Toplevel() (see the comments)'00c714' with '#00c714'"login")now it becomes "compile" time error-free. As for your question, 2 things need changing:
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With