How do I create an input box with Python?
Asked Answered
N

4

11

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?

Needle answered 1/1, 2014 at 2:17 Comment(1)
Related: Creating a popup message box with an Entry fieldNicety
M
17

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.

Monacid answered 1/1, 2014 at 2:23 Comment(3)
I think he wanted a simple example, like myText=tkSimpleDialog.askstring("Title","Enter a string:"). This is the most simple solution, however, I never used it.Bimetallic
@Sasszem Your suggested code errors with NameError: name 'tkSimpleDialog' is not definedNicety
You need to import it first, with import tkSimpleDialogBimetallic
A
3

The Tk library actually has a function that does this although it is nominally 'private'. You can use it as follows.

import tkinter as tk

root = tk.Tk()
root.wm_geometry("800x600")
dialog = tk.Toplevel(root)
root_name = root.winfo_pathname(root.winfo_id())
dialog_name = dialog.winfo_pathname(dialog.winfo_id())
root.tk.eval('tk::PlaceWindow {0} widget {1}'.format(dialog_name, root_name))
root.mainloop()

This will place your dialog centred over the specified window (in this case the root window). Ref.

In a real life application

Here's a version I made because in real life you'll need to show an InputBox over the top of the Main Window/Form, and typically in a Modal (can't click on other windows) state, and then close it when the user clicks OK:

try:
    # for Python2
    import Tkinter as tk
except ImportError:
    # for Python3
    import tkinter as tk

class App:
    def __init__(self):
        self.HEIGHT = 700
        self.WIDTH = 800    
        root = tk.Tk()
        root.width = self.WIDTH
        root.height = self.HEIGHT
        self.dialogroot = root
        self.strDialogResult = ""    
        self.canvas = tk.Canvas(root, height=self.HEIGHT, width=self.WIDTH)
        self.canvas.pack()    
        frame = tk.Frame(root, bg='#42c2f4')
        frame.place(relx=0.5, rely=0.02, relwidth=0.96, relheight=0.95, anchor='n')  
        # Here is the button call to the InputBox() function
        buttonInputBox = tk.Button(frame, text="Input Box", bg='#cccccc', font=60, 
        command=lambda: self.InputBox())    
        buttonInputBox.place(relx=0.05, rely=0.1, relwidth=0.90, relheight=0.8)    
        root.mainloop()

    def InputBox(self):        
        dialog = tk.Toplevel(self.dialogroot)
        dialog.width = 600
        dialog.height = 100

        frame = tk.Frame(dialog,  bg='#42c2f4', bd=5)
        frame.place(relwidth=1, relheight=1)

        entry = tk.Entry(frame, font=40)
        entry.place(relwidth=0.65, rely=0.02, relheight=0.96)
        entry.focus_set()

        submit = tk.Button(frame, text='OK', font=16, command=lambda: self.DialogResult(entry.get()))
        submit.place(relx=0.7, rely=0.02, relheight=0.96, relwidth=0.3)

        root_name = self.dialogroot.winfo_pathname(self.dialogroot.winfo_id())
        dialog_name = dialog.winfo_pathname(dialog.winfo_id())

        # These two lines show a modal dialog
        self.dialogroot.tk.eval('tk::PlaceWindow {0} widget {1}'.format(dialog_name, root_name))
        self.dialogroot.mainloop()

        #This line destroys the modal dialog after the user exits/accepts it
        dialog.destroy()

        #Print and return the inputbox result
        print(self.strDialogResult)
        return self.strDialogResult

    def DialogResult(self, result):
        self.strDialogResult = result
        #This line quits from the MODAL STATE but doesn't close or destroy the modal dialog
        self.dialogroot.quit()


# Launch ...
if __name__ == '__main__':
    app = App()
Acetylcholine answered 8/9, 2019 at 6:40 Comment(0)
C
-1

In the IDLE you can already ask the user for an input, like here where i have done a maths equation to be solved...

answer = int(input('What is the answer to 1+2?'))
def equation(answer):
    if answer == 3:
        print ('Correct!')
    else:
        print ('Wrong, it is 3!')

then call the function...

equation(answer)
Clemente answered 15/7, 2020 at 6:26 Comment(1)
The question was: "on-screen input box" Your suggestion is for console, nor for GUI.Banquer
J
-4

The simplest way to do it is to set an input equal to a variable for later use, and then call the variable later in the program.

variable = str(input("Type into the text box."))
Juryman answered 5/12, 2017 at 19:34 Comment(1)
str is not necessary. Input already returns a string.Tabasco

© 2022 - 2024 — McMap. All rights reserved.