from tkinter import *
from tkinter import messagebox
w=Tk()
w.geometry('350x500')
w.title('Sunset Panel')
w.resizable(0,0)
#Making gradient frame
j=0
r=10
for i in range(100):
c=str(222222 r)
Frame(w,width=10,height=500,bg="#" c).place(x=j,y=0)
j=j 10
r=r 1
Frame(w,width=250,height=400,bg='white').place(x=50,y=50)
l1=Label(w,text='Username',bg='white')
l=('Consolas',13)
l1.config(font=l)
l1.place(x=80,y=200)
#e1 entry for username entry
e1=Entry(w,width=20,border=0)
l=('Consolas',13)
e1.config(font=l)
e1.place(x=80,y=230)
#e2 entry for password entry
e2=Entry(w,width=20,border=0,show='*')
e2.config(font=l)
e2.place(x=80,y=310)
l2=Label(w,text='Password',bg='white')
l=('Consolas',13)
l2.config(font=l)
l2.place(x=80,y=280)
###lineframe on entry
Frame(w,width=180,height=2,bg='#141414').place(x=80,y=332)
Frame(w,width=180,height=2,bg='#141414').place(x=80,y=252)
from PIL import ImageTk,Image
imagea=Image.open("log.png")
imageb= ImageTk.PhotoImage(imagea)
label1 = Label(image=imageb,
border=0,
justify=CENTER)
label1.place(x=115, y=50)
#Command
def cmd():
db = open("Data.txt", "r")
Username = e1
Password = e2
if Username == e1:
d = []
f = []
for i in db:
a,b = i.split(", ")
b = b.strip()
d.append(a)
f.append(b)
data = dict(zip(d, f))
if e1.get() == data[Username] and e2.get() == data[Password]:
messagebox.showinfo("Login success", "Welcome")
q=Tk()
q.mainloop()
else:
messagebox.showwarning("Login failed dumb ass","Try again goofy")
#Button_with hover effect
def bttn(x,y,text,ecolor,lcolor):
def on_entera(e):
myButton1['background'] = ecolor #ffcc66
myButton1['foreground']= lcolor #000d33
def on_leavea(e):
myButton1['background'] = lcolor
myButton1['foreground']= ecolor
myButton1 = Button(w,text=text,
width=20,
height=2,
fg=ecolor,
border=0,
bg=lcolor,
activeforeground=lcolor,
activebackground=ecolor,
command=cmd)
myButton1.bind("<Enter>", on_entera)
myButton1.bind("<Leave>", on_leavea)
myButton1.place(x=x,y=y)
bttn(100,375,'L O G I N','white','#994422')
w.mainloop()
my error is:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\ezneu\anaconda3\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\ezneu\OneDrive\Desktop\login\login.py", line 80, in cmd
if e1.get() == data[Username] and e2.get() == data[Password]:
KeyError: <tkinter.Entry object .!entry>
sorry for the dumb question ive been at this all day and cant seem to figure out what the problem is I sent the entire code above if someone can correct! I keep getting the same " KeyError: <tkinter.Entry object .!entry>" no clue what this means but I even watched a tutorial and my code was almost the same as the person in the tut yet there worked perfect please help!
CodePudding user response:
Since Username
is a reference to an Entry
widget and data
is a dictionary with string as the keys, so data[Username]
will raise the exception.
Your checking should test whether e1.get()
is a key in data
and e2.get()
should be equal to data[e1.get()]
.
Below is the suggested modification of cmd()
:
def cmd():
username = e1.get().strip()
password = e2.get().strip()
# both username and password have been input?
if username and password:
# load the credentials as a dictionary
with open("Data.txt") as db:
data = {}
for i in db:
a, b = i.split(", ")
data[a] = b.strip()
# check username and password
if username in data and data[username] == password:
messagebox.showinfo("Login Success", "Welcome")
# do whatever you want if login successful
else:
messagebox.showwarning("Login Failed", "Try again")
else:
messagebox.showwarning("Invalid", "Please enter both username and password")
CodePudding user response:
https://realpython.com/python-keyerror/ :
A Python KeyError
exception is what is raised when you try to access a key that isn’t in a dictionary ( in your case data
).
Here an example of content of the file 'Data.txt':
adam, a
bogdan, b
cicero, c
daniel, d
Below a making sense code of cmd()
running without the error message:
def cmd():
db = open("Data.txt", "r")
d = []
f = []
for i in db.readlines():
print (i)
a, b = i.split(", ")
b = b.strip()
d.append(a)
f.append(b)
data = dict(zip(d, f))
print(data)
try:
if data[e1.get()] == e2.get():
messagebox.showinfo("Login success", "Welcome")
q=Tk()
q.mainloop()
else:
messagebox.showwarning("Wrong password entered", "Try again")
except KeyError:
messagebox.showwarning("Login failed dumb ass","Try again goofy")
And here entire code with the gradient frame which looks like it should:
from tkinter import *
w=Tk()
w.geometry('350x500')
w.title('Sunset Panel')
w.resizable(0,0)
#Making gradient frame
j=0; r=10
for _ in range(35):
c=str(f'{155-r:02x}{255-r:02x}{155-r:02x}')
print(c,end='|')
Frame(w,width=10,height=500,bg="#" c).place(x=j,y=0)
j=j 10
r=r 3
Frame(w,width=250,height=400,bg='white').place(x=50,y=50)
l1=Label(w,text='Username',bg='white')
l=('Consolas',13)
l1.config(font=l)
l1.place(x=80,y=200)
#e1 entry for username entry
e1=Entry(w,width=13,border=0)
l=('Consolas',13)
e1.config(font=l)
e1.place(x=80,y=230)
#e2 entry for password entry
e2=Entry(w,width=13,border=0,show='*')
e2.config(font=l)
e2.place(x=80,y=310)
l2=Label(w,text='Password',bg='white')
l=('Consolas',13)
l2.config(font=l)
l2.place(x=80,y=280)
###lineframe on entry
Frame(w,width=180,height=2,bg='#141414').place(x=80,y=332)
Frame(w,width=180,height=2,bg='#141414').place(x=80,y=252)
from PIL import ImageTk,Image
imagea=Image.open("log.png")
imageb= ImageTk.PhotoImage(imagea)
label1 = Label(image=imageb, border=0, justify=CENTER)
label1.place(x=115, y=50)
#Command
def cmd():
db = open("Data.txt", "r")
d = []
f = []
for i in db.readlines():
print (i)
a, b = i.split(", ")
b = b.strip()
d.append(a)
f.append(b)
data = dict(zip(d, f))
print(data)
try:
if data[e1.get()] == e2.get():
messagebox.showinfo("Login success", "Welcome")
q=Tk()
q.mainloop()
else:
messagebox.showwarning("Wrong password entered", "Try again")
except KeyError:
messagebox.showwarning("Login failed dumb ass","Try again goofy")
#Button_with hover effect
def bttn(x,y,text,ecolor,lcolor):
def on_entera(e):
myButton1['background'] = ecolor #ffcc66
myButton1['foreground']= lcolor #000d33
def on_leavea(e):
myButton1['background'] = lcolor
myButton1['foreground']= ecolor
myButton1 = Button(w,text=text,
width=20,
height=2,
fg=ecolor,
border=0,
bg=lcolor,
activeforeground=lcolor,
activebackground=ecolor,
command=cmd)
myButton1.bind("<Enter>", on_entera)
myButton1.bind("<Leave>", on_leavea)
myButton1.place(x=x,y=y)
bttn(100,375,'L O G I N','white','#994422')
w.mainloop()