105105buttonsFrame = None
106106
107107
108- def _alertTkinter (text = "" , title = "" , button = OK_TEXT , root = None , timeout = None ):
108+ def _alertTkinter (text = "" , title = "" , button = OK_TEXT , root = None , timeout = None , icon = None ):
109109 """Displays a simple message box with text and a single OK button. Returns the text of the button clicked on."""
110110 assert TKINTER_IMPORT_SUCCEEDED , "Tkinter is required for pymsgbox"
111111 text = str (text )
112112 retVal = _buttonbox (
113- msg = text , title = title , choices = [str (button )], root = root , timeout = timeout
113+ msg = text , title = title , choices = [str (button )], root = root , timeout = timeout , icon = icon
114114 )
115115 if retVal is None :
116116 return button
@@ -122,7 +122,7 @@ def _alertTkinter(text="", title="", button=OK_TEXT, root=None, timeout=None):
122122
123123
124124def _confirmTkinter (
125- text = "" , title = "" , buttons = (OK_TEXT , CANCEL_TEXT ), root = None , timeout = None
125+ text = "" , title = "" , buttons = (OK_TEXT , CANCEL_TEXT ), root = None , timeout = None , icon = None
126126):
127127 """Displays a message box with OK and Cancel buttons. Number and text of buttons can be customized. Returns the text of the button clicked on."""
128128 assert TKINTER_IMPORT_SUCCEEDED , "Tkinter is required for pymsgbox"
@@ -133,29 +133,30 @@ def _confirmTkinter(
133133 choices = [str (b ) for b in buttons ],
134134 root = root ,
135135 timeout = timeout ,
136+ icon = icon
136137 )
137138
138139
139140confirm = _confirmTkinter
140141
141142
142- def _promptTkinter (text = "" , title = "" , default = "" , root = None , timeout = None ):
143+ def _promptTkinter (text = "" , title = "" , default = "" , root = None , timeout = None , icon = None ):
143144 """Displays a message box with text input, and OK & Cancel buttons. Returns the text entered, or None if Cancel was clicked."""
144145 assert TKINTER_IMPORT_SUCCEEDED , "Tkinter is required for pymsgbox"
145146 text = str (text )
146147 return __fillablebox (
147- text , title , default = default , mask = None , root = root , timeout = timeout
148+ text , title , default = default , mask = None , root = root , timeout = timeout , icon = icon
148149 )
149150
150151
151152prompt = _promptTkinter
152153
153154
154- def _passwordTkinter (text = "" , title = "" , default = "" , mask = "*" , root = None , timeout = None ):
155+ def _passwordTkinter (text = "" , title = "" , default = "" , mask = "*" , root = None , timeout = None , icon = None ):
155156 """Displays a message box with text input, and OK & Cancel buttons. Typed characters appear as *. Returns the text entered, or None if Cancel was clicked."""
156157 assert TKINTER_IMPORT_SUCCEEDED , "Tkinter is required for pymsgbox"
157158 text = str (text )
158- return __fillablebox (text , title , default , mask = mask , root = root , timeout = timeout )
159+ return __fillablebox (text , title , default , mask = mask , root = root , timeout = timeout , icon = icon )
159160
160161
161162password = _passwordTkinter
@@ -181,7 +182,22 @@ def timeoutBoxRoot():
181182 __enterboxText = TIMEOUT_RETURN_VALUE
182183
183184
184- def _buttonbox (msg , title , choices , root = None , timeout = None ):
185+ def _seticon (window : tk .Tk , icon : str ):
186+ try :
187+ img = tk .PhotoImage (file = icon )
188+ window .iconphoto (True , img )
189+ return
190+ except tk .TclError :
191+ pass
192+
193+ try :
194+ window .iconbitmap (icon )
195+ return
196+ except tk .TclError :
197+ pass
198+
199+
200+ def _buttonbox (msg , title , choices , root = None , timeout = None , icon = None ):
185201 """
186202 Display a msg, a title, and a set of buttons.
187203 The buttons are defined by the members of the choices list.
@@ -190,6 +206,7 @@ def _buttonbox(msg, title, choices, root=None, timeout=None):
190206 @arg msg: the msg to be displayed.
191207 @arg title: the window title
192208 @arg choices: a list or tuple of the choices to be displayed
209+ @arg icon: the window icon (tk bitmap name, png, gif, ico or xbm file)
193210 """
194211 global boxRoot , __replyButtonText , __widgetTexts , buttonsFrame
195212
@@ -205,6 +222,9 @@ def _buttonbox(msg, title, choices, root=None, timeout=None):
205222 boxRoot = tk .Tk ()
206223 boxRoot .withdraw ()
207224
225+ if icon :
226+ _seticon (boxRoot , icon )
227+
208228 boxRoot .title (title )
209229 boxRoot .iconname ("Dialog" )
210230 boxRoot .geometry (rootWindowPosition )
@@ -311,7 +331,7 @@ def __cancelButtonEvent(event):
311331 boxRoot .quit ()
312332
313333
314- def __fillablebox (msg , title = "" , default = "" , mask = None , root = None , timeout = None ):
334+ def __fillablebox (msg , title = "" , default = "" , mask = None , root = None , timeout = None , icon = None ):
315335 """
316336 Show a box in which a user can enter some text.
317337 You may optionally specify some default text, which will appear in the
@@ -337,6 +357,9 @@ def __fillablebox(msg, title="", default="", mask=None, root=None, timeout=None)
337357 boxRoot = tk .Tk ()
338358 boxRoot .withdraw ()
339359
360+ if icon :
361+ _seticon (boxRoot , icon )
362+
340363 boxRoot .title (title )
341364 boxRoot .iconname ("Dialog" )
342365 boxRoot .geometry (rootWindowPosition )
0 commit comments