Creating User Input Dialog With Python GUI Programming

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()

ROOT.withdraw()
# 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

Explanation

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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close