Skip to content
← Back to Community
How to create a prompt(w/out Tkinter) in Python
Profile icon
yeetuscleetus

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.

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): os.system("clear")

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

print(text+":\n") for x in choices: if choices[CurrentOption] == x: cprint(choices[CurrentOption],"white","on_blue") else: cprint(x,"white")

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 = { "enter":"'\\r'", "up":"'\\x1b[A'", "down":"'\\x1b[B'" } e = repr(readkey()) if e == keys["enter"]: os.system("clear") return choices[CurrentOption] elif e == keys["up"]: if CurrentOption == 0: CurrentOption = len(choices)-1 return prompt(text,choices,len(choices)-1) else: return prompt(text,choices,CurrentOption-1) elif e == keys["down"]: if CurrentOption == len(choices)-1: CurrentOption = 0 return prompt(text,choices,0) else: 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).

Voters
Profile icon
ThroneUnknown
Profile icon
mkhoi
Profile icon
Jakman
Profile icon
XanderEhlert
Profile icon
Codemonkey51
Profile icon
Navinor
Profile icon
yeetuscleetus
Comments
hotnewtop
Profile icon
ThusEndingTheStory

Are there any more keys that I could use?

Profile icon
Navinor

lol good job

Profile icon
yeetuscleetus

thanks

Profile icon
Navinor

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

Profile icon
yeetuscleetus

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

Profile icon
Navinor

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