Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to create a prompt(w/out Tkinter) in Python
yeetuscleetus (37)

You've probably wondered how to create prompts without using input functions all the time. And what if Tkinter doesn't seem to work?

Well, fear no more! This tutorial will show you how to create a prompt box using a couple of modules.


To create a prompt box, you'll only need 3 modules: os,readchar, and termcolor:

from os import system
from readchar import readkey
from termcolor import cprint

Now what?

The first thing I recommend is creating a function prompt with arguments text, choices, and CurrentOption(that'll be important later) as follows:

def prompt(text,choices,CurrentOption = 0):

So, we have an empty function. How do we display the text?
It's as simple as this:

for x in choices:
   if choices[CurrentOption] == x:

Ok...that might've or might not have been complicated, depending on how much of an expert you are. I'll explain it for those who don't understand:

First, the title is printed out. Then, the program loops over the choices list, and the currently selected option(choices[CurrentOption]) is highlighted blue.

Detecting keypresses

 keys = {
  e = repr(readkey())
  if e == keys["enter"]:
    return choices[CurrentOption]
  elif e == keys["up"]:
    if CurrentOption == 0:
      CurrentOption = len(choices)-1
      return prompt(text,choices,len(choices)-1)
      return prompt(text,choices,CurrentOption-1)
  elif e == keys["down"]:
    if CurrentOption == len(choices)-1:
      CurrentOption = 0
      return prompt(text,choices,0)
     return prompt(text,choices,CurrentOption+1)
  elif e not in keys:
    return prompt(text,choices,CurrentOption)           

So, what does this code mean? It checks if the enter, up arrow, and down arrow keys are pressed. If the key you pressed isn't in that list: don't worry! It won't do anything to your prompt.

And...well, that's pretty much it!

I have to shoutout @Warhawk947, all because the prompt he made was too complicated for me to understand(I'm so sorry).

Warhawk947 (527)

@yeetuscleetus also, just so I can know, what part of my prompt thing did you find confusing? I hope that I can help you

yeetuscleetus (37)

the first time i read it i couldn't understand anything
maybe it's because i skimmed over it lmao

Warhawk947 (527)

@yeetuscleetus well, you don't really need to understand it...