Python Forum
Problem with input after function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem with input after function (/thread-35659.html)

Pages: 1 2


Problem with input after function - luilong - Nov-27-2021

Good evening to everyone,
I've been learning Python for a few months and i'm experiencing a trouble that seriously
I'm not able to understand, so i hope someone can kindly help me !

I'm tring to do this stuff:

def login_form():
    print("--Login Form--")
    input("Username")
    input("Password")

def intro_menu_key_enter():
    global intro_index_selected
    
    if intro_index_selected == 1:
        cls()
        login_form()
    elif intro_index_selected == 2:
        cls()
        registration_form()
    else:
        cls()
        exit()
in this def, i call the login_form() function and the problem is that the first input ( input("Username") )
ia already "entered" so i can just insert the second one input ( input("Password") ).

[Image: Immagine-2021-11-27-210652.png]

When I call the function automatically the first input was automatically entered.
Please if you know, could you kidnly explain to me how is possible ?!?

Thank you in advance !


RE: Problem with input after function - BashBedlam - Nov-27-2021

I'm not sure whatcls()is but it seems to be the source of you difficulty. Without it your code works as expected.
intro_index_selected = 1

def login_form():
	print("--Login Form--")
	input("Username")
	input("Password")
 
def intro_menu_key_enter():
	global intro_index_selected
	 
	if intro_index_selected == 1:
		login_form()
	elif intro_index_selected == 2:
		registration_form()
	else:
		exit()

intro_menu_key_enter ()



RE: Problem with input after function - luilong - Nov-28-2021

Hello Bash,
thank you very much for you reply, I really much appreciated.

I tried your tips, but nothing changed. The problem is always here.
Anyway I just have found the problem Dance , but i need always an help.

The trouble is here :

keyboard.add_hotkey('enter', intro_menu_key_enter)
If i change "enter" with other keys like "A", "space", etc the problem disappear.
In this way, the line username input it' available and the problem disappear.

As you can assume, it's difficoult change key and solve the problem,
because it's normal for user to press "enter" key for submit data.

Let me know if you ( or someone ) have any idea !

Thank you in advance


RE: Problem with input after function - ghoul - Nov-28-2021

(Nov-28-2021, 12:19 AM)luilong Wrote:
keyboard.add_hotkey('enter', intro_menu_key_enter)

What exactly are you trying to do? Do you really need the above?


RE: Problem with input after function - luilong - Nov-28-2021

Hi ghoul, the situation is the following:

[Image: 00.png]

the code in this case is the following:

    
def intro_menu():
    intro_menu_create()
    keyboard.add_hotkey('up', intro_menu_key_up)
    keyboard.add_hotkey('down', intro_menu_key_down)
    keyboard.add_hotkey('space', intro_menu_key_enter)
    keyboard.wait()

def intro_menu_create():
    global intro_index_selected

    intro_menu = ["Accedi", "Registrati", "Esci"]

    cls()
    ascii_art()
    print("\n" * 3)

    print(" Accedi \ Registrati")
    for i in range(1, 4):
        print("{1} {0} {2}".format(intro_menu[i-1], ">" if intro_index_selected == i else " ", "<" if intro_index_selected == i else " "))

def intro_menu_key_up():
    global intro_index_selected
    if intro_index_selected == 1:
        return
    intro_index_selected -= 1
    intro_menu_create()

def intro_menu_key_down():
    global intro_index_selected
    if intro_index_selected == 3:
        return
    intro_index_selected += 1
    intro_menu_create()

def intro_menu_key_enter():
    global intro_index_selected
    
    if intro_index_selected == 1:
        login_form()
    elif intro_index_selected == 2:
        registration_form()
    else:
        exit()
In this "menu function" when user press 'up' or 'down' keys,
the cursors '>' and '<' moves up and down to previous or following
menu item in according to the index "intro_index_selected" value, updating the print.

In this function for wait and catch the key pressed by the user i call the keyboard.wait() function.
When user press Enter key, thru the function "keyboard.add_hotkey('enter', intro_menu_key_enter)"
the relative function is called as shown below:

def intro_menu_key_enter():
    global intro_index_selected
    
    if intro_index_selected == 1:
        login_form()
    elif intro_index_selected == 2:
        registration_form()
    else:
        exit()
Now the problem exist when i call login_form() after have obtained
the 'enter' key thru keyboard.add_hotkey('enter', intro_menu_key_enter).

The login_form() was correctly called, but the first input was just "auto-validate" with empty value,
meanwhile is just possible to write in the second input.

If I choose another key instead of 'enter', it works properly ..
I've no idea where I could looking for the solution Shifty


RE: Problem with input after function - deanhystad - Nov-29-2021

Next time you post you should post a working example. At least that would show the imports. You should have mentioned that you are using a keyboard package. Maybe this one?

https://pypi.org/project/keyboard/

It seems obvious to me that making enter a hot key is going to break input() which uses enter to signal the end of typing. If you want to use this package you might need to make your own input-like function. The pressed_keys.py example looks like a good start for something like this.

https://github.com/boppreh/keyboard/blob/master/examples/pressed_keys.py


RE: Problem with input after function - luilong - Nov-29-2021

(Nov-29-2021, 04:30 PM)deanhystad Wrote: Next time you post you should post a working example. At least that would show the imports. You should have mentioned that you are using a keyboard package. Maybe this one?

https://pypi.org/project/keyboard/

It seems obvious to me that making enter a hot key is going to break input() which uses enter to signal the end of typing. If you want to use this package you might need to make your own input-like function. The pressed_keys.py example looks like a good start for something like this.

https://github.com/boppreh/keyboard/blob/master/examples/pressed_keys.py

Hello deanhystad, i'm trying to find a solution, but also with hook function,
the problem is always the same.

import keyboard

def presentation():
        input("Name: ")
        input("Surname: ")

def my_keyboard_hook(keyboard_event):
    if keyboard_event.name == "enter":
        print("You have pressed the key: " + keyboard_event.name)
        presentation()

    elif keyboard_event.name == "space":
        print("You have pressed the key: " + keyboard_event.name)

keyboard.on_press(my_keyboard_hook)
keyboard.wait()
Could you check this code and kindly tell me what i'm wrong !? please ...


RE: Problem with input after function - deanhystad - Nov-29-2021

You should ask the author of the keyboard package. I don't understand why you are using the keyboard package at all.


RE: Problem with input after function - luilong - Nov-29-2021

(Nov-29-2021, 09:38 PM)deanhystad Wrote: You should ask the author of the keyboard package. I don't understand why you are using the keyboard package at all.

Hi deanhystad, thank you for reply. I realy appreciated your tips about submit the question to package productor,
and i done it on github. I'm using the keyboard package for give to my menu, a little touch of "interactivity".

when menu page is loaded, the keyboard.wait() stand for user input for handle the preset key available
( arrow up, down and enter ) moving the cursor up and down in the menu,
printing the menu page again with moved cursor by user.

If i'dont use a menu like this, menu page could be very boring Cry

That's the reason why 'enter' key pressure need to be catched by keyboard module
because it's not a simple menu with free input from user.

Testing different solution the problem is also available even when i replace the 'enter' key with 'space' key for example,
because when I hit any key, hook(), on_press(), on_release() or any other function of keyboard package,
seems to catch the keys pressure and release, so i'll have two consecutive value even with one key pressure.
Using 'space' input isn't submited, but the values catched after one key pressure, is also double.

Thank you in advance for any other tips ( if you've )


RE: Problem with input after function - deanhystad - Nov-30-2021

What about curses? I get the imptession that keyboard is more for automation than input.