The python tkMessagebox for displaying output, errors, warnings and choice selections

Monday, May 25, 2009

This is perhaps the easiest python Tk widget to work with, the tkMessageBox which is used to display informational, error, warning and choice selection messages, however, this is a different module, though you can import widgets like Labels, Text, OptionMenu, Entry widgets from Tkinter, the tkMessagebox is another python module which creates a root Tkinter window and displays a dialogue box with the message type (say info, error, warnings, question and so on) with the message text.

Though this is a trivial code, but I want to keep a reference of it to know the different methods of tkMessageBox module which may be useful for GUI development in python, lets see the code.


1.  # File name: tkMessageBoxDemo.py
2. # Author: S.Prasanna
3.
4. from Tkinter import *
5. import tkMessageBox
6.
7. if __name__ == "__main__":
8.
9. root = Tk()
10. root.title("tkMessageBox Demo Widget")
11. root["padx"] = 20
12. root["pady"] = 20
13.
14. tkinterLabel = Label(root)
15. tkinterLabel["text"] = "tkMessageBox demo running...."
16. tkinterLabel.pack()
17.
18. tkMessageBox.showinfo(title="Tk Info box", \
19. message="This is a Tk Info/Message box used to display output")
20.
21. tkMessageBox.showerror(title="Tk Error message box", \
22. message="This is a Tk Error Message box used to display errors")
23.
24. tkMessageBox.showwarning(title="Tk Warning message box", \
25. message="This is a Tk Warning Message box used to display warnings")
26.
27. if (tkMessageBox.askokcancel(title="Tk Ok/Cancel box", \
28. message="Select an option, OK or Cancel") == 1):
29. tkMessageBox.showinfo(title="Tk Ok/Cancel box", \
30. message="You selected OK option")
31. else:
32. tkMessageBox.showinfo(title="Tk Ok/Cancel box", \
33. message="You selected Cancel option")
34.
35. if (tkMessageBox.askretrycancel(title="Tk Retry/Cancel box", \
36. message="Select an option, Retry or Cancel") == 1):
37. tkMessageBox.showinfo(title="Tk Retry/Cancel box", \
38. message="You selected Retry option")
39. else:
40. tkMessageBox.showinfo(title="Tk Retry/Cancel box", \
41. message="You selected Cancel option")
42.
43. if (tkMessageBox.askyesno(title="Tk Yes/No box", \
44. message="Select an option, Yes or No") == 1):
45. tkMessageBox.showinfo(title="Tk Yes/No box", \
46. message="You selected Yes option")
47. else:
48. tkMessageBox.showinfo(title="Tk Yes/No box", \
49. message="You selected No option")
50.
51. if (tkMessageBox.askquestion(title="Tk Question box", \
52. message="Is 1 + 1 = 2") == "yes"):
53. tkMessageBox.showinfo(title="Tk Question box", \
54. message="Correct Answer")
55. else:
56. tkMessageBox.showinfo(title="Tk Question box", \
57. message="Wrong Answer")
58.
59. tkinterLabel["text"] = "tkMessageBox demo complete...."
60.
61. root.mainloop()
62.
Sample Output:

Figure 1: tkMessageBox Info, Ok/Cancel option displays

Explanation:

The code is quite simple and most part of it are self explanatory, however the askokcancel (line 27), askretrycancel (line 35) and askyesno (line 43) methods of tkMessageBox module displays choices to the end user and returns True(1) or False (0) depending on the user's selection, for the askyesno method, Yes choice returns a True value and a No choice returns False with a similar behavior for askretrycancel and askokcancel. There is a slight difference in the values returned by the tkMessageBox's askquestion method (line 51) which displays a couple of choices for the user (Yes and No) and returns yes/no respectively according to the user's selection.

3 comments:

Theajo said...

Hi Prasana..
That was a cool demo..
It was useful for me..
Cheers
Theajo

Prasanna Seshadri said...

Thanks for saying so, glad that it was useful to you.

EvilInside said...

Thanks for your Demo. Keep it Up. Cheers !!!!!!!!!!!


Copyright © 2008 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.