The Tkinter simpledialog module provides small modal dialogs for collecting simple user input. Use askstring() for text, askinteger() for integers and askfloat() for floating-point numbers.
askstring() returns a string, askinteger() returns an integer and askfloat() returns a float. All three return None when the user clicks Cancel.
simpledialog is a Tkinter module. Import it from tkinter:
import tkinter as tk
from tkinter import simpledialog
The three commonly used input functions are:
| Function | Accepted Input | Returns |
|---|---|---|
askstring() | Text | str or None |
askinteger() | Integer | int or None |
askfloat() | Integer or decimal number | float or None |
askinteger() accepts valid integer input. If the entered value is not a valid integer, the dialog displays a validation message and asks again.
import tkinter as tk
from tkinter import simpledialog
root=tk.Tk()
root.geometry('410x180')
root.title('plus2net.com')
age=simpledialog.askinteger('Input', 'Enter your age:', parent=root)
print(age)
root.mainloop()
If the user enters:
25
the returned value is the integer 25.
if age is not None:
print(type(age))
Output
<class 'int'>
Use askfloat() when the application needs a floating-point number.
price=simpledialog.askfloat('Input', 'Enter the price:', parent=root)
if price is not None:
print(price)
print(type(price))
If the user enters:
125.75
the returned value is a Python float.
An integer-looking value such as:
100
is returned as:
100.0
askstring() accepts text and returns it as a Python string.
name=simpledialog.askstring('Input', 'Enter your name:', parent=root)
if name is not None:
print(name)
print(type(name))
If the user enters digits such as 123, askstring() still returns the string '123'.
The simple input dialogs accept several useful options.
| Option | Purpose |
|---|---|
title | Text displayed in the dialog title bar. |
prompt | Message asking the user for input. |
parent | Parent Tkinter window for the modal dialog. |
initialvalue | Initial value shown in the input field. |
minvalue | Minimum accepted numeric value for askinteger() or askfloat(). |
maxvalue | Maximum accepted numeric value for askinteger() or askfloat(). |
show | Character used to mask text in askstring(), for example '*'. |
quantity=simpledialog.askinteger('Quantity', 'Enter quantity:', parent=root, initialvalue=1)
The dialog opens with 1 already displayed in the input field.
askinteger() and askfloat() provide built-in numeric range validation.
mark=simpledialog.askinteger('Marks', 'Enter mark from 1 to 100:', parent=root, minvalue=1, maxvalue=100)
If the user enters a value outside the allowed range, the dialog displays an error and remains open.
price=simpledialog.askfloat('Price', 'Enter price:', parent=root, minvalue=0.0, maxvalue=10000.0)
This accepts a numeric value only within the specified range.

askinteger() and askfloat() validate both the data type and optional numeric limits. askstring() accepts text, so additional application-specific validation must be written separately when required.All three simple input functions return None if the user clicks Cancel.
name=simpledialog.askstring('Input', 'Enter your name:', parent=root)
if name is None:
print('User cancelled the dialog.')
else:
print('Entered:', name)
This condition:
if name:
treats both None and an empty string '' as false.
If Cancel and an empty string need to be handled differently, use:
if name is None:
print('Cancelled')
elif name == '':
print('No text entered')
else:
print(name)

The returned string can be displayed in a Tkinter Label.
import tkinter as tk
from tkinter import simpledialog
root=tk.Tk()
root.geometry('410x180')
root.title('plus2net.com')
name=simpledialog.askstring('Input', 'Enter your name:', parent=root)
if name is not None:
label=tk.Label(root, text=f'Welcome {name}', font=('Times', 24))
label.grid(row=0, column=0, padx=10, pady=20)
root.mainloop()

The None check is necessary before using the result in the Label.
In many GUI applications, opening the dialog from a Button gives a clearer user experience than displaying it immediately when the program starts.
import tkinter as tk
from tkinter import simpledialog
def ask_name():
name=simpledialog.askstring('Name', 'Enter your name:', parent=root)
if name is not None:
result.config(text=f'Welcome {name}')
root=tk.Tk()
root.geometry('350x180')
tk.Button(root, text='Enter Name', command=ask_name).pack(pady=20)
result=tk.Label(root, text='')
result.pack()
root.mainloop()

A dialog can also be opened when an Entry widget receives keyboard focus. Because the dialog itself temporarily changes focus, unbind the event before opening the dialog and bind it again after the dialog closes.
import tkinter as tk
from tkinter import simpledialog
def open_dialog(event):
entry.unbind('<FocusIn>')
value=simpledialog.askstring('Input', 'Enter your name:', parent=root)
if value is not None:
name_var.set(value)
entry.bind('<FocusIn>', open_dialog)
root=tk.Tk()
root.geometry('410x160')
name_var=tk.StringVar()
entry=tk.Entry(root, textvariable=name_var, width=20, font=('Times', 20))
entry.grid(row=0, column=0, padx=10, pady=20)
entry.bind('<FocusIn>', open_dialog)
root.mainloop()

askstring() supports the show option. This replaces displayed characters with another character while the user types.
password=simpledialog.askstring('Login', 'Enter password:', parent=root, show='*')
The returned value is still the actual string entered by the user.
| Module | Use |
|---|---|
simpledialog | Collect a simple string, integer or floating-point value from the user. |
messagebox | Display information, warnings, errors or simple Yes/No/OK/Cancel choices. |
For example, use simpledialog.askinteger() when a number must be entered, but use a messagebox when the application only needs to display a warning or ask for confirmation.
simpledialog is a Tkinter module containing input-dialog functions and dialog classes.
This can fail:
name=simpledialog.askstring('Input', 'Name:')
message='Welcome ' + name
If Cancel is clicked, name is None.
Use:
if name is not None:
message='Welcome ' + name
if value: also treats empty strings and numeric zero as false. Use is None when detecting Cancel.
If the user enters 123, askstring() returns '123', not integer 123.
For simple numeric limits, use minvalue and maxvalue instead of writing unnecessary validation code.
Passing parent=root keeps the dialog associated with its Tkinter parent window and provides more predictable modal behaviour.
simpledialog is intended for simple one-value input. For forms containing several fields, create a custom Tkinter window or dialog instead.
simpledialog is a Tkinter module for simple modal input dialogs.askstring() for text input.askinteger() for integer input.askfloat() for floating-point input.askstring() returns str or None.askinteger() returns int or None.askfloat() returns float or None.None when Cancel is pressed.is None or is not None to distinguish Cancel accurately.initialvalue supplies a default value.minvalue and maxvalue validate numeric ranges.show='*' masks characters displayed by askstring().parent associates the modal dialog with its Tkinter window.messagebox for information, alerts and confirmation rather than typed input.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.