Creating User Input Dialog With Python GUI Programming

1 min read

In this tutorial, we will create a dialog which takes input from the user and prints it in the terminal, the purpose of this tutorial is to understand how to take the user input for GUI application.

We will use the built-in Python package Tkinter it is implemented as a Python wrapper for the Tcl Interpreter embedded within the interpreter of Python.

Creating User Input Dialog With Tkinter

import tkinter as tk
from tkinter import simpledialog

ROOT = tk.Tk()

# the input dialog
USER_INP = simpledialog.askstring(title="Test",
                                  prompt="What's your Name?:")

# check it out
print("Hello", USER_INP)

Save the file you should see the following input dialog asking for your name.

Creating user input dialog with python and TKinter
Enter your name here it should be printed in the terminal along with the message.

Hello Tony


First, we are importing the Tkinter module, then we are creating a window in the ROOT object.

Next, we have the withdraw() method which removes the window from the screen (without destroying it).

Later we are taking the user from the user using askstring() method which simply takes the string entered.

At the bottom, we printing out the Hello string along with the user input.


Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read