Getting input in python via the same window?

Viewed 979

I would like to know how to get user input in python without using the command line or an input box. Let me explain. I do not want to do this

#All code is python 3
name=input("What is your name?")

Why? When running scripts, the command line is not auto-focused. Furthermore, it pops up another window, something I do not want because I can't hit escape to close it in a hurry (Something which you may want to do if you're playing a game).

What have I tried? I looked at WX and it's dialog function, something like this:

import wx
app=wx.App()
def text_entry(title,message):
    result=None
    dlg=wx.TextEntryDialog(None, message,title)
    if dlg.ShowModal()==wx.ID_OK: result=dlg.GetValue()
    dlg.Destroy()
    return result
text_entry("Text entry","Enter something here")

While this works, it pops up another window which again, I do not want. However, it is closer to what I am ultimately looking for, because I can hit escape to make it go away.

I have tried using pygame and it's key.get_pressed() function, but it inserts a lot of the same letter into the entry, even if I gently tap the key. Also, when I implemented it into the project, it can only pick up on normal letters. Writing 26 if statements to detect key presses for a single letter with or without the shift key seems a little counter intuitive. Finally, I am a bit hesitant to try tkinter. I happen to be blind, and from what I read, tk is very visual, which makes me concerned that it won't play nicely with my screen reader (NVDA).

So, I'm here. After searching on google for "getting input without using command line in python 3", "input in the same window", and "input without using input()" yielded nothing.

To recap, I want to accept user input without using the input() function, and without any additional windows popping up for the duration of me doing so. Thank you.

2 Answers

What about this solution using the msvcrt module. At any time if you press escape then the program will exit. Python sys.exit(), and built-ins exit() and quit() all call raise SystemExit so this is just one less call to perform. If you press the enter or return key then the while loop ends and you can use the keys that were pressed later in your program as they are stored in the variable user_input. The print at the end just proves that the pressed keys are stored in user_input variable and the input() function simply to leave the window open so you can see it working.

import msvcrt


user_input = b''

while True:
    pressed_key = msvcrt.getche()  # getch() will not echo key to window if that is what you want
    if pressed_key == b'\x1b':  # b'\x1b' is escape
        raise SystemExit
    elif pressed_key == b'\r':  # b'\r' is enter or return
        break
    else:
        user_input += pressed_key

print('\n' + user_input.decode('utf-8'))  # this just shows you that user_input variable can be used now somewhere else in your code
input()  # input just leaves the window open so you can see before it exits you may want to remove
Related