Basic python login failing due to runtime error

Question:

When running the below code, the error RuntimeError: can't re-enter readline keeps appearing (line 6 – ‘username = input…’).

I am trying to create a basic pop-up login window that closes when the details entered match the stored value. If the entered details are incorrect, the window stays and the user can try again.

from tkinter import *
import tkinter as tk

def login_function():

    username = input('Please enter your username:')
    password = input('Please enter your password:')
    print("Checking details")

    if username == 'testu' and password == 'testp':
        print('Username is right')
        winLogin.destroy()
    
    else:
        print('Incorrect username')
        #username.delete()
        #password.delete()
        login_function()
    
    return

#Create window and format
winLogin = Tk()
winLogin.geometry('400x250')  
winLogin.title('Login Page')

#Login labels
usernameLabel = Label(winLogin, text='Username').grid(row=0, column=0)
passwordLabel = Label(winLogin, text='Password').grid(row=1, column=0)

username = ()
password = ()

#Login entry fields
usernameEntry = Entry(winLogin, textvariable=username).grid(row=0, column=1)
passwordEntry = Entry(winLogin, textvariable=password, show='*').grid(row=1, column=1)

#Login buttons
loginButton = Button(winLogin, text='Login', command=login_function).grid(row=4, column=0)

login_function()
Asked By: JavaPuff

||

Answers:

From https://github.com/matplotlib/matplotlib/issues/10061#issuecomment-359972287

If you are doing this in a terminal, you are using readline for the prompt and it is sitting there waiting for the user to type. input is also going to use readline to try and get user input. When you hit a key, Tk fires an event (in c) that eventually ends up calling your python function (from inside the tk event loop). You now have two places in your code trying use readline in the same process which is slightly un-defined

Hope that helps in some way.

Answered By: puncher
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.