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

Django 4.1 adds async-compatible interface to QuerySet

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - The Django core team has been progressively adding async suppor…
Read more →

3 min read

Making Django Admin Jazzy With django-jazzmin

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
Read more →

4 min read