[Python-checkins] r67240 - sandbox/trunk/tkinter-polo/src/Dialog.py
guilherme.polo
python-checkins at python.org
Mon Nov 17 01:14:11 CET 2008
Author: guilherme.polo
Date: Mon Nov 17 01:14:07 2008
New Revision: 67240
Log:
Dropped usage of star import;
No need to check for TkVersion <= 3.6 since _tkinter enforces the need for
Tcl/Tk 8.2 at least;
Cleaned up the Dialog class;
Correctly converts the reply to an integer, checking for the possibility of
receiving a tuple;
Updated example to suggest the more appropriate use of the Dialog.
Modified:
sandbox/trunk/tkinter-polo/src/Dialog.py
Modified: sandbox/trunk/tkinter-polo/src/Dialog.py
==============================================================================
--- sandbox/trunk/tkinter-polo/src/Dialog.py (original)
+++ sandbox/trunk/tkinter-polo/src/Dialog.py Mon Nov 17 01:14:07 2008
@@ -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()
More information about the Python-checkins
mailing list