Tkinter SimpleDialog

This article covers Tkinter SimpleDialog, which is a sub class of Tkinter.

The SimpleDialog module is used to create dialog boxes to take input from the user in a variety of ways. SimpleDialog allows us to take input of varying datatypes from the user, such as float, string and integer.

The below Tkinter SimpleDialog functions work together with tkinter. A tkinter window is required for these functions to work. If a tkinter window is not present, these functions will create one by default.


SimpleDialog – askinteger

The askinteger() function is used to get an integer from the user. It accepts only int values.

from tkinter.simpledialog import askinteger

root = tk.Tk()
prompt = askinteger("Input", "Input an Integer")
print(prompt)

root.mainloop()
CodersLegacy simpledialog askinteger

SimpleDialog – askfloat

The askfloat() function is used to get float values from the user. It accepts integer values, but returns them in float format.

from tkinter.simpledialog import askfloat

root = tk.Tk()
prompt = askfloat("Input", "Input an Float")
print(prompt)

root.mainloop()
CodersLegacy simpledialog askfloat

SimpleDialog – askstring

The askstring() function is used to get a string from the user. You can pass values of any type to it, but it will return them in string format.

from tkinter.simpledialog import askstring

root = tk.Tk()
prompt = askstring("Input", "Input an String")
print(prompt)

root.mainloop()
CodersLegacy simpledialog askstring

An interesting side note. If you pass an string value into the float function for instance, it will not throw an exception and terminate the program. A simple error prompt will be shown, and the program will continue as normal.

If you only want to show a simple dialog with only text (no inputs), then take a look at the Tkinter messagebox widget.


This marks the end of the Tkinter SimpleDialog article. Suggestions or contributions for CodersLegacy are more than welcome. Any questions can be asked in the comments section below.

Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments