--- Dialog.py 2008/11/16 10:09:44 67231 +++ Dialog.py 2008/11/17 00:14:07 67240 @@ -1,49 +1,63 @@ -# dialog.py -- Tkinter interface to the tk_dialog script. +""" +Tkinter interface to the command tk_dialog in tk. +""" -from Tkinter import * -from Tkinter import _cnfmerge +__all__ = ['Dialog', 'DIALOG_ICON'] -if TkVersion <= 3.6: - DIALOG_ICON = 'warning' -else: - DIALOG_ICON = 'questhead' +import Tkinter +DIALOG_ICON = 'questhead' + +class Dialog(Tkinter.Toplevel): + """ + Create modal dialog and wait for response. + """ + widgetname = 'tk_dialog' -class Dialog(Widget): def __init__(self, master=None, cnf={}, **kw): - cnf = _cnfmerge((cnf, kw)) - self.widgetName = '__dialog__' - Widget._setup(self, master, cnf) - self.num = self.tk.getint( - self.tk.call( - 'tk_dialog', self._w, - cnf['title'], cnf['text'], - cnf['bitmap'], cnf['default'], - *cnf['strings'])) - try: Widget.destroy(self) - except TclError: pass - def destroy(self): pass - -def _test(): - d = Dialog(None, {'title': 'File Modified', - 'text': - 'File "Python.h" has been modified' - ' since the last time it was saved.' - ' Do you want to save it before' - ' exiting the application.', - 'bitmap': DIALOG_ICON, - 'default': 0, - 'strings': ('Save File', - 'Discard Changes', - 'Return to Editor')}) - print d.num + """Construct a tk_dialog with the parent master and the parameters + in kw. + kw must contain the following keys: + title, text, bitmap, default, strings + """ + Tkinter.Toplevel.__init__(self, master) + kw.update(cnf) + args = tuple(kw[arg] for arg in ('title', 'text', 'bitmap', 'default')) + strings = kw['strings'] + + result = self.tk.call(self.widgetname, self._w, *(args + strings)) + if isinstance(result, tuple): + # We may receive a tuple with a single Tcl_Obj, so convert that to + # a string before continuing. + result = str(result[0]) + self.num = int(result) + + try: + self.destroy() + except Tkinter.TclError: + pass + + +def example(): + def test_dialog(): + d = Dialog( + title='File Modified', + text=("File 'Python.h' has been modified since the last time " + "it was saved. Do you want to save it before exiting " + "the application ?"), + bitmap=DIALOG_ICON, + strings=('Save File', 'Discard Changes', 'Return to Editor'), + default=0 + ) + print d.num + + from Tkinter import Button + test = Button(text='Test', command=test_dialog) + quit = Button(text='Quit', command=test.quit) + test.pack() + quit.pack() + test.mainloop() if __name__ == '__main__': - t = Button(None, {'text': 'Test', - 'command': _test, - Pack: {}}) - q = Button(None, {'text': 'Quit', - 'command': t.quit, - Pack: {}}) - t.mainloop() + example()