0

I was making login system and if they use incorrect password or username and if they left the input field empty it should say error in order to do that I use get attribute but it's saying object has no attribute get so what should I do?

from tkinter import *
from PIL import ImageTk
from tkinter import messagebox
class Login():
    def __init__(self, root):
        self.root = root
        self.root.title("Login system")
        self.root.geometry("1920x1080")

        #background img
        self.bg=ImageTk.PhotoImage(file="Image/1.jpg")
        self.bg_image=Label(self.root, image=self.bg).place(x=0, y=0, relwidth=1, relheight=1)
        
        #login page
        FrameLogin = Frame(self.root, bg="White")
        FrameLogin.place(x=390, y=150, width=500, height=488)

        #LoginTitle
        title=Label(FrameLogin, text="Login Here", font=("Impact", 35, "bold"), fg="#850101", bg="white").place(x=130, y=30)

        #Username
        UserUserName=Label(FrameLogin, text="Username", font=("Impact", 15), fg="black", bg="white").place(x=70, y=160)
        self.UserName=Entry(FrameLogin, font=("Goudy old style", 15), fg="grey", bg="white").place(x=70, y=190, width=320, height=35)
       
        #Password
        UserPassword=Label(FrameLogin, text="Password", font=("Impact", 15,), fg="black", bg="white").place(x=70, y=230)
        self.Password=Entry(FrameLogin, font=("Goudy old style", 15), fg="grey", bg="white").place(x=70, y=260, width=320, height=35)

        #ForgetButton
        ForgetButton = Button(FrameLogin, text="Forget Password", bd=0, font=("Goudy old style", 10), fg="blue", bg="white").place(x=70, y=310)

        #LoginButton
        LoginButton = Button(FrameLogin, command=self.checker, cursor="hand2", text="Login", bd=0, font=("Impact", 20), fg="white", bg="#850101").place(x=120, y=350, width=230, height= 37)

    def checker(self):
        if self.UserName.get() == "" or self.Password.get() == "":
            messagebox.showerror("Error", "Input required", parent=self.root)
        elif self.UserName.get() != "Abemelek" or self.Password.get() != "1234":
            messagebox.showerror("Eror", "Username or password incorrect", parent=self.root)
        elif self.UserName.get() == "Abemelek" or self.Password.get() == "1234":
            messagebox("Welcome", f"Welcome {self.UserName.get()}")


root = Tk()
obj = Login(root)
root.mainloop()
  • 1
    Does this answer your question? [Why do I get "AttributeError: NoneType object has no attribute" using Tkinter? Where did the None value come from?](https://stackoverflow.com/questions/1101750/why-do-i-get-attributeerror-nonetype-object-has-no-attribute-using-tkinter-w) – JRiggles Apr 28 '23 at 18:20
  • I'm guessing there are a couple way to do this, and it's been a while since I've used tkinter, but you need to set something like `self.name = StringVar()` and `self.password = StringVar()` inside of your constructor, and then add `textvariable=self.name` and `textvariable=self.password` to your UserName and Password entry boxes respectively (e.g. `self.UserName=Entry(... , textvariable=self.name, ... )`. Also, you need to call `self.name.get()` instead of `self.UserName.get()` as UserName is just the entry widget. – nulzo Apr 28 '23 at 18:55
  • @JRiggles Sill the same – Abemelek Ermias Apr 28 '23 at 19:04
  • There're a couple more things I noticed: you probably want an `and` in your last elif statement in `checker()`. Although the elif statement above it is *technically* taking that into account, you could reduce the code a bit. Also, your last messagebox won't display anything since you aren't calling any method on it (such as `messagebox.showinfo(...)`). – nulzo Apr 28 '23 at 19:07
  • @nulzo thank you so much but it only works if i leave the entry field empty – Abemelek Ermias Apr 28 '23 at 19:29
  • @AbemelekErmias hmm, let me post a small code snippet as an answer below so you can see how it is formatted. – nulzo Apr 28 '23 at 19:33

2 Answers2

1

You need to split two lines.

    #Username
    UserUserName=Label(FrameLogin, text="Username", font=("Impact", 15), fg="black", bg="white")
    UserUserName.place(x=70, y=160)
    self.UserName=Entry(FrameLogin, font=("Goudy old style", 15),fg="grey",bg="white")
        self.UserName.place(x=70, y=190, width=320, height=35)
       
        #Password
        UserPassword=Label(FrameLogin, text="Password", font=("Impact", 15,), fg="black", bg="white").place(x=70, y=230)
        self.Password=Entry(FrameLogin, font=("Goudy old style", 15), fg="grey", bg="white")
        self.Password.place(x=70, y=260, width=320, height=35)

Don't use or for checker() function. Use and.

def checker(self):
    if self.UserName.get() == "" and self.Password.get() == "":
        messagebox.showerror("Error", "Input required", parent=self.root)
    elif self.UserName.get() != "Abemelek" and self.Password.get() != "1234":
        messagebox.showerror("Eror", "Username or password incorrect", parent=self.root)
    elif self.UserName.get() == "Abemelek" and self.Password.get() == "1234":
        messagebox("Welcome", f"Welcome {self.UserName.get()}")

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • I post some of my answer https://stackoverflow.com/questions/73090561/im-trying-to-make-a-sign-up-form-and-then-a-log-in-form/76129205#76129205 – toyota Supra Apr 28 '23 at 19:40
0

Here is what I was thinking.

class Login():
    def __init__(self, root):

        ...

        self.name = StringVar()
        self.password = StringVar()
        
        ...

        #Username
        self.UserUserName=Label(FrameLogin, text="Username", font=("Impact", 15), fg="black", bg="white").place(x=70, y=160)
        self.UserName=Entry(FrameLogin, textvariable=self.name, font=("Goudy old style", 15), fg="grey", bg="white").place(x=70, y=190, width=320, height=35)
       
        #Password
        self.UserPassword=Label(FrameLogin, text="Password", font=("Impact", 15,), fg="black", bg="white").place(x=70, y=230)
        self.Password=Entry(FrameLogin, textvariable=self.password, font=("Goudy old style", 15), fg="grey", bg="white").place(x=70, y=260, width=320, height=35)

        ...

And, for checker().

def checker(self):
        if self.name.get() == "" or self.password.get() == "":
            messagebox.showerror("Error", "Input required", parent=self.root)
        elif self.name.get() == "Abemelek" and self.password.get() == "1234":
            messagebox.showinfo("Welcome", f"Welcome {self.name.get()}", parent=self.root)
        else:
            messagebox.showerror("Eror", "Username or password incorrect", parent=self.root)

Let me know if you have any questions.

nulzo
  • 96
  • 6