1
# tk common message boxes
3
# this module provides an interface to the native message boxes
4
# available in Tk 4.2 and newer.
6
# written by Fredrik Lundh, May 1997
10
# options (all have default values):
12
# - default: which button to make default (one of the reply codes)
14
# - icon: which icon to display (see below)
16
# - message: the message to display
18
# - parent: which window to place the dialog on top of
20
# - title: dialog title
22
# - type: dialog type; that is, which buttons to display (see below)
25
from tkinter.commondialog import Dialog
37
ABORTRETRYIGNORE = "abortretryignore"
40
RETRYCANCEL = "retrycancel"
42
YESNOCANCEL = "yesnocancel"
55
# message dialog class
57
class Message(Dialog):
60
command = "tk_messageBox"
66
# Rename _icon and _type options to allow overriding them in options
67
def _show(title=None, message=None, _icon=None, _type=None, **options):
68
if _icon and "icon" not in options: options["icon"] = _icon
69
if _type and "type" not in options: options["type"] = _type
70
if title: options["title"] = title
71
if message: options["message"] = message
72
res = Message(**options).show()
73
# In some Tcl installations, yes/no is converted into a boolean.
74
if isinstance(res, bool):
78
# In others we get a Tcl_Obj.
81
def showinfo(title=None, message=None, **options):
82
"Show an info message"
83
return _show(title, message, INFO, OK, **options)
85
def showwarning(title=None, message=None, **options):
86
"Show a warning message"
87
return _show(title, message, WARNING, OK, **options)
89
def showerror(title=None, message=None, **options):
90
"Show an error message"
91
return _show(title, message, ERROR, OK, **options)
93
def askquestion(title=None, message=None, **options):
95
return _show(title, message, QUESTION, YESNO, **options)
97
def askokcancel(title=None, message=None, **options):
98
"Ask if operation should proceed; return true if the answer is ok"
99
s = _show(title, message, QUESTION, OKCANCEL, **options)
102
def askyesno(title=None, message=None, **options):
103
"Ask a question; return true if the answer is yes"
104
s = _show(title, message, QUESTION, YESNO, **options)
107
def askyesnocancel(title=None, message=None, **options):
108
"Ask a question; return true if the answer is yes, None if cancelled."
109
s = _show(title, message, QUESTION, YESNOCANCEL, **options)
110
# s might be a Tcl index object, so convert it to a string
116
def askretrycancel(title=None, message=None, **options):
117
"Ask if operation should be retried; return true if the answer is yes"
118
s = _show(title, message, WARNING, RETRYCANCEL, **options)
122
# --------------------------------------------------------------------
125
if __name__ == "__main__":
127
print("info", showinfo("Spam", "Egg Information"))
128
print("warning", showwarning("Spam", "Egg Warning"))
129
print("error", showerror("Spam", "Egg Alert"))
130
print("question", askquestion("Spam", "Question?"))
131
print("proceed", askokcancel("Spam", "Proceed?"))
132
print("yes/no", askyesno("Spam", "Got it?"))
133
print("yes/no/cancel", askyesnocancel("Spam", "Want it?"))
134
print("try again", askretrycancel("Spam", "Try again?"))