Message boxes are windows that pop up to ask you a question or say something and then go away. tkinter provides a set of dialogues (dialog in American English spelling), which can be used to display message boxes, showing warning or errors, or widgets to select files and colors. There are also simple dialogues, asking the user to enter string, integers or float numbers.
There are a variety of different types of message boxes. For each of them you can specify the message the user will see as well as the title of the message box. The following program creates three message boxes as shown in the output:
from tkinter import *
from tkinter.messagebox import *
master = Tk()
master.title('Message Box')
showinfo(title='Message for you', message='Hi There!')
askquestion(title='Quit?', message='Do you really want to quit?')
showwarning(title='Warning', message='Unsupported format')
mainloop()
The output is shown below:
Once you click the OK button the output changes to:
If you click Yes or No, the output becomes:
A practical implementation of the message box is shown in the program below:
from tkinter import *
from tkinter.messagebox import *
master = Tk()
master.title('Message Box')
def answer():
showerror("Answer", "Sorry, no answer available")
def callback():
if askyesno('Verify', 'Wanna quit?'):
import sys; sys.exit()
else:
showinfo('No', 'You wish to continue')
b1 = Button(text='Quit', command=callback)
b2 = Button(text='Answer', command=answer)
b1.pack()
b2.pack()
mainloop()
The output of the program is shown below:
If Answer button is clicked, the output changes to:
If Quit button is selected then the output becomes:
If no is clicked the output becomes:
If yes was selected the application exits. The message dialogues are provided by the tkMessageBox module which consists of the following functions corresponding to dialog windows:
1. askokcancel(title=None, message=None, **options)
Ask if operation should proceed; return true if the answer is ok
2. askquestion(title=None, message=None, **options)
Ask a question
3. askretrycancel(title=None, message=None, **options)
Ask if operation should be retried; return true if the answer is yes
4. askyesno(title=None, message=None, **options)
Ask a question; return true if the answer is yes
5. askyesnocancel(title=None, message=None, **options)
Ask a question; return true if the answer is yes, None if cancelled.
6. showerror(title=None, message=None, **options)
Show an error message
7. showinfo(title=None, message=None, **options)
Show an info message
8. showwarning(title=None, message=None, **options)
Show a warning message
Each of these functions returns a value indicating what the user clicked.
Try to use the other options shown above in your programs to understand their functionality. I'll end this post now and till we meet next, keep learning and practicing Python as Python is easy to learn!
There are a variety of different types of message boxes. For each of them you can specify the message the user will see as well as the title of the message box. The following program creates three message boxes as shown in the output:
from tkinter import *
from tkinter.messagebox import *
master = Tk()
master.title('Message Box')
showinfo(title='Message for you', message='Hi There!')
askquestion(title='Quit?', message='Do you really want to quit?')
showwarning(title='Warning', message='Unsupported format')
mainloop()
The output is shown below:
Once you click the OK button the output changes to:
If you click Yes or No, the output becomes:
A practical implementation of the message box is shown in the program below:
from tkinter import *
from tkinter.messagebox import *
master = Tk()
master.title('Message Box')
def answer():
showerror("Answer", "Sorry, no answer available")
def callback():
if askyesno('Verify', 'Wanna quit?'):
import sys; sys.exit()
else:
showinfo('No', 'You wish to continue')
b1 = Button(text='Quit', command=callback)
b2 = Button(text='Answer', command=answer)
b1.pack()
b2.pack()
mainloop()
The output of the program is shown below:
If Answer button is clicked, the output changes to:
If Quit button is selected then the output becomes:
If no is clicked the output becomes:
If yes was selected the application exits. The message dialogues are provided by the tkMessageBox module which consists of the following functions corresponding to dialog windows:
1. askokcancel(title=None, message=None, **options)
Ask if operation should proceed; return true if the answer is ok
2. askquestion(title=None, message=None, **options)
Ask a question
3. askretrycancel(title=None, message=None, **options)
Ask if operation should be retried; return true if the answer is yes
4. askyesno(title=None, message=None, **options)
Ask a question; return true if the answer is yes
5. askyesnocancel(title=None, message=None, **options)
Ask a question; return true if the answer is yes, None if cancelled.
6. showerror(title=None, message=None, **options)
Show an error message
7. showinfo(title=None, message=None, **options)
Show an info message
8. showwarning(title=None, message=None, **options)
Show a warning message
Each of these functions returns a value indicating what the user clicked.
Try to use the other options shown above in your programs to understand their functionality. I'll end this post now and till we meet next, keep learning and practicing Python as Python is easy to learn!
0 comments:
Post a Comment