[Python-checkins] r74487 - in python/branches/tk_and_idle_maintenance/Lib/idlelib: EditorWindow.py textView.py

guilherme.polo python-checkins at python.org
Mon Aug 17 14:39:49 CEST 2009


Author: guilherme.polo
Date: Mon Aug 17 14:39:49 2009
New Revision: 74487

Log:
Make the IDLE help dialog non-modal. Patch from issue #964437.

Modified:
   python/branches/tk_and_idle_maintenance/Lib/idlelib/EditorWindow.py
   python/branches/tk_and_idle_maintenance/Lib/idlelib/textView.py

Modified: python/branches/tk_and_idle_maintenance/Lib/idlelib/EditorWindow.py
==============================================================================
--- python/branches/tk_and_idle_maintenance/Lib/idlelib/EditorWindow.py	(original)
+++ python/branches/tk_and_idle_maintenance/Lib/idlelib/EditorWindow.py	Mon Aug 17 14:39:49 2009
@@ -50,6 +50,33 @@
             raise ImportError, 'No source for module ' + module.__name__
     return file, filename, descr
 
+class _singledialog(object):
+    """Just a given dialog should be executing at any time. Trying to
+    create a new one results in bringing to front the running dialog."""
+
+    def __init__(self, meth):
+        self.meth = meth
+        self.dlg = None
+
+    def __get__(self, instance, owner):
+        self.instance = instance
+        return self
+
+    def __call__(self, *args):
+        if self.dlg: # dialog is already running
+            # bring it to front
+            self.dlg.withdraw()
+            self.dlg.deiconify()
+            self.dlg.lift()
+
+        else: # dialog not running, start it and save the instance
+            self.dlg = self.meth(self.instance, *args)
+            self.dlg.bind('<Destroy>', self._clear_dlg)
+
+    def _clear_dlg(self, *args):
+        """Dialog is being destroyed. A new dialog instance can be created."""
+        self.dlg = None
+
 class EditorWindow(object):
     from Percolator import Percolator
     from ColorDelegator import ColorDelegator
@@ -478,9 +505,10 @@
     def config_dialog(self, event=None):
         configDialog.ConfigDialog(self.top,'Settings')
 
+    @_singledialog
     def help_dialog(self, event=None):
         fn=os.path.join(os.path.abspath(os.path.dirname(__file__)),'help.txt')
-        textView.view_file(self.top,'Help',fn)
+        return textView.view_file(self.top, 'Help', fn, modal=False)
 
     def python_docs(self, event=None):
         if sys.platform[:3] == 'win':

Modified: python/branches/tk_and_idle_maintenance/Lib/idlelib/textView.py
==============================================================================
--- python/branches/tk_and_idle_maintenance/Lib/idlelib/textView.py	(original)
+++ python/branches/tk_and_idle_maintenance/Lib/idlelib/textView.py	Mon Aug 17 14:39:49 2009
@@ -9,7 +9,7 @@
     """A simple text viewer dialog for IDLE
 
     """
-    def __init__(self, parent, title, text):
+    def __init__(self, parent, title, text, modal=True):
         """Show the given text in a scrollable window with a 'close' button
 
         """
@@ -24,8 +24,6 @@
 
         self.CreateWidgets()
         self.title(title)
-        self.transient(parent)
-        self.grab_set()
         self.protocol("WM_DELETE_WINDOW", self.Ok)
         self.parent = parent
         self.textView.focus_set()
@@ -34,7 +32,11 @@
         self.bind('<Escape>',self.Ok) #dismiss dialog
         self.textView.insert(0.0, text)
         self.textView.config(state=DISABLED)
-        self.wait_window()
+
+        if modal:
+            self.transient(parent)
+            self.grab_set()
+            self.wait_window()
 
     def CreateWidgets(self):
         frameText = Frame(self, relief=SUNKEN, height=700)
@@ -57,10 +59,10 @@
         self.destroy()
 
 
-def view_text(parent, title, text):
-    TextViewer(parent, title, text)
+def view_text(parent, title, text, modal=True):
+    return TextViewer(parent, title, text, modal)
 
-def view_file(parent, title, filename, encoding=None):
+def view_file(parent, title, filename, encoding=None, modal=True):
     try:
         if encoding:
             import codecs
@@ -73,21 +75,30 @@
                                message='Unable to load file %r .' % filename,
                                parent=parent)
     else:
-        return view_text(parent, title, textFile.read())
+        return view_text(parent, title, textFile.read(), modal)
 
 
 if __name__ == '__main__':
     #test the dialog
+    import os
     root=Tk()
     root.title('textView test')
-    filename = './textView.py'
+    filename = os.path.abspath(__file__)
     text = file(filename, 'r').read()
+
     btn1 = Button(root, text='view_text',
-                 command=lambda:view_text(root, 'view_text', text))
+                  command=lambda:view_text(root, 'view_text', text))
     btn1.pack(side=LEFT)
+
     btn2 = Button(root, text='view_file',
                   command=lambda:view_file(root, 'view_file', filename))
     btn2.pack(side=LEFT)
+
+    btn3 = Button(root, text='nonmodal view_text',
+                  command=lambda:view_text(root, 'nonmodal view_text', text,
+                                           modal=False))
+    btn3.pack(side=LEFT)
+
     close = Button(root, text='Close', command=root.destroy)
     close.pack(side=RIGHT)
     root.mainloop()


More information about the Python-checkins mailing list