How do I create an input box with Python?

Lev picture Lev · Jan 1, 2014 · Viewed 88k times · Source

I want to create an on-screen input box that a user can interact with.

The user would see a window with an input field they can click with the mouse. The user could type or erase text in the field, then press OK once they have finished adding text. Lastly, my program would store this text for later use.

How can I create a text box in Python which allows user input?

Answer

Christian picture Christian · Jan 1, 2014

You could try the Tkinter module:

from tkinter import *

master = Tk()
e = Entry(master)
e.pack()

e.focus_set()

def callback():
    print e.get() # This is the text you may want to use later

b = Button(master, text = "OK", width = 10, command = callback)
b.pack()

mainloop()

Result:

Result

Of course, you may want to read a Tkinter tutorial.