Tkinter simpledialog: askstring(), askinteger() and askfloat()

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.

Tkinter simpledialog askinteger input dialog


Tkinter simpledialog for Integer, Float and String Input

Import Tkinter simpledialog 🔝

simpledialog is a Tkinter module. Import it from tkinter:

import tkinter as tk
from tkinter import simpledialog

The three commonly used input functions are:

FunctionAccepted InputReturns
askstring()Textstr or None
askinteger()Integerint or None
askfloat()Integer or decimal numberfloat or None

askinteger(): Get Integer Input 🔝

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.

Check the Returned Type

if age is not None:
    print(type(age))
Output
<class 'int'>

askfloat(): Get Decimal Input 🔝

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(): Get Text Input 🔝

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'.

Common simpledialog Options 🔝

The simple input dialogs accept several useful options.

OptionPurpose
titleText displayed in the dialog title bar.
promptMessage asking the user for input.
parentParent Tkinter window for the modal dialog.
initialvalueInitial value shown in the input field.
minvalueMinimum accepted numeric value for askinteger() or askfloat().
maxvalueMaximum accepted numeric value for askinteger() or askfloat().
showCharacter used to mask text in askstring(), for example '*'.

Using initialvalue

quantity=simpledialog.askinteger('Quantity', 'Enter quantity:', parent=root, initialvalue=1)

The dialog opens with 1 already displayed in the input field.

Validate Numeric Input with minvalue and maxvalue 🔝

askinteger() and askfloat() provide built-in numeric range validation.

Accept an Integer from 1 to 100

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.

Validate a Price

price=simpledialog.askfloat('Price', 'Enter price:', parent=root, minvalue=0.0, maxvalue=10000.0)

This accepts a numeric value only within the specified range.

Tkinter simpledialog validation error for invalid numeric input

Built-in validation: 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.

Handle the Cancel Button Correctly 🔝

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)

Why not use if 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)

Display simpledialog Input in a Label 🔝

Tkinter simpledialog askstring dialog

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()
Displaying Tkinter simpledialog input in a Label

The None check is necessary before using the result in the Label.

Open simpledialog from a Button 🔝

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

Open simpledialog When an Entry Gets Focus 🔝

Opening Tkinter simpledialog when Entry receives focus

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()
Displaying simpledialog result in Tkinter Entry

Mask Text Input with askstring(show='*') 🔝

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.

Masking only hides the characters visually while typing. It does not by itself provide secure password storage or authentication.

simpledialog vs messagebox 🔝

ModuleUse
simpledialogCollect a simple string, integer or floating-point value from the user.
messageboxDisplay 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.

Common Tkinter simpledialog Mistakes 🔝

1. Calling simpledialog a Class

simpledialog is a Tkinter module containing input-dialog functions and dialog classes.

2. Forgetting to Handle Cancel

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

3. Using if value to Detect Cancel

if value: also treats empty strings and numeric zero as false. Use is None when detecting Cancel.

4. Expecting askstring() to Convert Numbers

If the user enters 123, askstring() returns '123', not integer 123.

5. Manually Validating Numeric Ranges

For simple numeric limits, use minvalue and maxvalue instead of writing unnecessary validation code.

6. Omitting parent

Passing parent=root keeps the dialog associated with its Tkinter parent window and provides more predictable modal behaviour.

7. Using simpledialog for Complex Forms

simpledialog is intended for simple one-value input. For forms containing several fields, create a custom Tkinter window or dialog instead.

Summary of Tkinter simpledialog 🔝

  • simpledialog is a Tkinter module for simple modal input dialogs.
  • Use askstring() for text input.
  • Use askinteger() for integer input.
  • Use askfloat() for floating-point input.
  • askstring() returns str or None.
  • askinteger() returns int or None.
  • askfloat() returns float or None.
  • All three return None when Cancel is pressed.
  • Use 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.
  • Use a Button to open dialogs when that gives users a clearer interaction.
  • Use messagebox for information, alerts and confirmation rather than typed input.
  • For multi-field forms, build a custom Tkinter dialog instead of using several simple dialogs.
Tkinter Messagebox Entry Widget StringVar() Label Widget




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer