7

I am using Tkinter to create a login screen here. At the moment, the "keep me logged in" button at the bottom is redundant and will remain so. What I want to do is use this code:

from tkinter import *

root = Tk()

label_1 = Label(root, text="Username")
label_2 = Label(root, text="Password")

entry_1 = Entry(root)
entry_2 = Entry(root)

label_1.grid(row=0, sticky=E)
label_2.grid(row=1, sticky=E)
entry_1.grid(row=0, column=1)
entry_2.grid(row=1, column=1)

checkbox = Checkbutton(root, text="Keep me logged in")
checkbox.grid(columnspan=2)

in conjunction with:

username = "john"
input("Username: ")
while not username:
    if username == "john":
        print("Welcome")
    else:
        print("User not found")


password = "password"
while not password:
    input("password: ")
    if password == "password":
        print("Logged in")
    else:
        print("Incorrect password")

However the logging in code does not work and then on top of that I do not know where to begin with integrating the two with each other. I am some what new to python and even more so to Tkinter but am desperate for this help!

Thanks in advance!

1
  • You need to make a method that changes password, then bind that method to your entry. Commented Jan 26, 2015 at 18:54

3 Answers 3

14

I extended your example. I made a class that holds your login window.

from tkinter import *
import tkinter.messagebox as tm


class LoginFrame(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.label_username = Label(self, text="Username")
        self.label_password = Label(self, text="Password")

        self.entry_username = Entry(self)
        self.entry_password = Entry(self, show="*")

        self.label_username.grid(row=0, sticky=E)
        self.label_password.grid(row=1, sticky=E)
        self.entry_username.grid(row=0, column=1)
        self.entry_password.grid(row=1, column=1)

        self.checkbox = Checkbutton(self, text="Keep me logged in")
        self.checkbox.grid(columnspan=2)

        self.logbtn = Button(self, text="Login", command=self._login_btn_clicked)
        self.logbtn.grid(columnspan=2)

        self.pack()

    def _login_btn_clicked(self):
        # print("Clicked")
        username = self.entry_username.get()
        password = self.entry_password.get()

        # print(username, password)

        if username == "john" and password == "password":
            tm.showinfo("Login info", "Welcome John")
        else:
            tm.showerror("Login error", "Incorrect username")


root = Tk()
lf = LoginFrame(root)
root.mainloop()

Sorry for not going over every single line what is happening there. I leave it to you to figure out. Its good exercise. But I will say that the most important is command = self._login_btn_clicked. This function will be executed when you click login button. In this function, you take the values of username and password, and check if they are correct. Also I did not attach any callbacks to the checkbox. But it would be similar to what is already done.

Edit: Edited as requested in the comments.

Login prompt

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

2 Comments

How would you open the main program window if the login was successful?
@Marcin I accidently downvoted your good answer when i meant to upvote. If you could edit your answer I would like to change my downvote to an upvote.
1

You probably want a 'Login' button, right? If you make that, you can bind a function to run when it is clicked using button's the command argument. In the function that the button calls you can do the checks for the correct username and password. Do not use the while loops though, just check once each time the button is pressed and respond accordingly.

Comments

0
from tkinter import *
from tkinter import messagebox


root = Tk()
root.title('Login')
root.geometry('925x500+300+200')
root.config(bg="#fff")
root.resizable(False, False)


# ---------Sign in Function-----------



def signin():
    username = user.get()
    password = code.get()

    if username == 'admin' and password == '1234':
        messagebox.showinfo("Success","Welcome")
        root.withdraw()
    elif username == "Username" and password == "Password":
        messagebox.showerror("Error", "Please Enter Username or Password")
    elif username != 'admin' and password != '1234':
        messagebox.showerror("Invalid", "Invalid Username or Password")
    elif username == 'admin' and password != '1234':
        messagebox.showerror("Error", "Please Enter Correct Password")
    elif username != 'admin' and password == '1234':
        messagebox.showerror("Error", "Please Enter Correct Username")


# ---------Image Here----------


img = PhotoImage(file='images/login.png')
Label(root, image=img, bg='white').place(x=50, y=50)

# ------------Frame-------------

frame = Frame(root, width=350, height=350, bg='white')
frame.place(x=480, y=70)

heading = Label(frame, text='Sign in', fg='#57a1f8', bg='white',
                font=('Microsoft YaHei UI Light', 23, 'bold'))
heading.place(x=100, y=5)

# ------------User Functions------------


def on_enter(e):
    user.delete(0, 'end')


def on_leave(e):
    name = user.get()
    if name == '':
        user.insert(0, 'Username')


# ------Entry-----------
user = Entry(frame, width=25, fg='black', border=0,
             bg='white', font=('Microsoft YaHei UI Light', 11))
user.place(x=30, y=80)
user.insert(0, 'Username')
user.bind('<FocusIn>', on_enter)
user.bind('<FocusOut>', on_leave)

Frame(frame, width=295, height=2, bg='black').place(x=25, y=107)

# ------------Password Functions------------


def on_enter(e):
    code.delete(0, 'end')


def on_leave(e):
    cname = code.get()
    if cname == '':
        code.insert(0, 'Password')


# -----------------------------------
code = Entry(frame, width=25, fg='black', border=0,
             bg='white', font=('Microsoft YaHei UI Light', 11))
code.place(x=30, y=150)
code.insert(0, 'Password')
code.bind('<FocusIn>', on_enter)
code.bind('<FocusOut>', on_leave)

Frame(frame, width=295, height=2, bg='black').place(x=25, y=177)

# ----------------------------------
Button(frame, width=39, pady=7, text='Sign in',
       bg='#57a1f8', fg='white', border=0, command=signin).place(x=35, y=204)

label = Label(frame, text="Don't have an account?", fg='black',
              bg='white', font=('Microsoft YaHei UI Light', 9))
label.place(x=75, y=270)


sign_up = Button(frame, width=6, text='Sign up', border=0,
                 bg='white', cursor='hand2', fg='#57a1f8')
sign_up.place(x=215, y=270)


root.mainloop()

login

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.