Python tkinter simpledialog()

Basics of Python Tkinter
Simpledialog askinteger
We can create dialog boxes and ask user inputs by using simpledialog class.

Tkinter simpledialog to collect user input in integer float or string and displaying in Label

askinteger()

import tkinter as tk
from tkinter import simpledialog
my_w = tk.Tk()
my_w.geometry("410x360")  
my_w.title("www.plus2net.com")  # Adding a title

my_i = simpledialog.askinteger("Input","Input an Integer",parent=my_w)
print(my_i)

my_w.mainloop()

askfloat()

We can collect float value like this. Here even we enter integer also we will get float data type. Try by entering string. Change the highlighted code above
my_f = simpledialog.askfloat("Input", "Input the price",parent=my_w)

askstring()

my_s = simpledialog.askstring("Input", "Input your Name",parent=my_w)

Validating the inputs

If we are using simpledialog.askinteger() then any float or string input will show error message. Similarly for simpledialog.askfloat() we can enter integer ( it will be converted to float data type) but we can’t enter any string.

For simpledialog.askstring() we can enter integer or float data types but it will store them as string.

Here is an example of message we will get if we enter string while using simpledialog.askintger() Illegal value message for wrong entry

Displaying entered string in Label

Simpledialog askstring

We can show the user entered text through simpledialog.askstring() in one Label in Parent window.
import tkinter as tk
from tkinter import simpledialog
my_w = tk.Tk()
my_w.geometry("410x360")  
my_w.title("www.plus2net.com")  # Adding a title
font1=('Times',30,'normal')

my_s = simpledialog.askstring("Input", "Input your Name",parent=my_w)

l1=tk.Label(my_w,font=font1,text="welcome "+my_s)
l1.grid(row=0,column=0,padx=10,pady=20)
print(my_s)

my_w.mainloop()
Simpledialog askstring and label

Opening Simpledialog on focus

Opening Simpledialog on focus of entery widget
In above codes the dialog box opens directly along with the parent window. Now we will try to open the dialog box when the entry widget is in focus.

On Submit of the dialog box , the data as entered by user will be displayed inside the Entry box.

Here we bind the FocusIn event of the entry box.
e1.bind("<FocusIn>", my_entry)
The function my_entry() triggers on focus and opens the Simpledialog box to take user input. The user input is collected using the my_str the StringVar which is connected to Entry widget.
import tkinter as tk
from tkinter import simpledialog
my_w = tk.Tk()
my_w.geometry("410x160")  
my_w.title("www.plus2net.com")  # Adding a title
font1=('Times',30,'normal')

def my_entry(*args):
    my_str.set(simpledialog.askstring("Input", "Input your Name",parent=my_w))

my_str=tk.StringVar()

e1=tk.Entry(my_w,font=font1,textvariable=my_str,width=15)
e1.grid(row=0,column=0,padx=10,pady=20)

e1.bind("<FocusIn>", my_entry)

my_w.mainloop()
Simpledialog string at input

Messagebox
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    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 FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer