simple dialog in tkinter // Taking input through dialog boxes
Tkinter : Simpledialog Widget
As you can see when i passed a string in ask integer method of simpledialog then a error message box pop-up's with a error message "Not an integer Please try again" and this error comes also when we try to pass float values to it .
Let you want to get some input from user then you use tkinter Entry widget , but if you want to take user input through a popup window then you have to use Top level class and the in it creates widgets like Entry , Button also set the geometry and other things .
So instead of using all these things you can just import a tkinter dialog widget simpledialog .
simpledialog is a standard dialog widget of tkinter like message box and file dialog .
Methods of simpledialog :
simple dialog have only 3 methods
1. askstring(title, prompt) :
For string type inputs
2. askinteger(title, prompt) :
For integer type inputs
3. askfloat(title, prompt) :
For floating type inputs
So now lets code :
from tkinter import * from tkinter import simpledialog def func(): data = simpledialog.askstring('Python says','Enter your Name') print(data) print(type(data)) root = Tk() root.title('Dynamic Coding') btn = Button(root,text='Press Here',command=func) btn.pack(pady = 10) root.mainloop()
OUTPUT :
As you can see wheni click on the function a small window pop-up's with a title " Python says " and message "Enter your name ".
And if i write my name and press ok then the value is printed according to the code written .
but if i pass a integer or a float number in it then what will happen .
so when you try this then you will get that output .
1. when string is passed
#Yogesh
#<class 'str'>
2 .when integer is passed
#1234
#<class 'str'>
3. when a float is passed
#12.4
#<class 'str'>
But if i try to pass a string in askinteger method then what will happen .
So now lets code :
from tkinter import * from tkinter import simpledialog def func(): data = simpledialog.askstring('Python says','Enter your Age') print(data) print(type(data)) root = Tk() root.title('Dynamic Coding') btn = Button(root,text='Press Here',command=func) btn.pack(pady = 10) root.mainloop()
OUTPUT :
The returned value is :
#None
#<class 'NoneType'>
The none value is also returned when we press cancel button of simpledialog .
Other tkinter related posts are :
This blog is contributed by Yogesh singh .
Please leave a feedback in comment section .
No comments