Mailing List Archive

Problem when using FileDialog from PMW Dialog. Quick fixes?
I have an application where I open a Python Mega Widget dialog (local,
not global) and within it I later open a FileDialog() for file selection
(Don't ask, why, but calling a nested dialog makes sense for the app.)
In any case, it doesn't work. When the FileDialog exits, the Pmw Dialog
cannot get focus back (even with some code to try to force it.)

Before I pursue this further, can anyone tell me if it is even worth
trying? It is nice to use the canned FileDialog, but its implementation
is somewhat crufty (IMHO) and I can probably work around it in other
ways (Creating my own FileDialog from PMW widgets is a nice possibility.
) But if there is a quick fix, I'd like to hear it.

Below you'll find some psuedo-code to show roughly what I'm doing. Run
the code, click on the "List" button to activate the FileDialog, then
exit the FileDialog. Hitting the PMW Dialog "Cancel" button doesn't
work (as it does if you never open the FileDialog). Pardon the rough
code :) You will probably have to kill the process if you trigger
the problem, so be warned.

Chad Netzer
chad@vision.arc.nasa.gov

------------------------------ [ Cut Here ]
------------------------------
from Tkinter import *
import Pmw
from FileDialog import LoadFileDialog

def CreatePMWDialog(parent):
global dialog
dialog = Pmw.Dialog(parent,
buttons = ('OK', 'Cancel'),
defaultbutton = 'OK',
title = 'A test: The FileDialog will steal focus
forever',
command = DialogAction)
dialog.parent = parent

interior = dialog.interior()
entry_frame = Frame(interior)
list_but = Button(entry_frame, text="List", command=ListFiles)

list_but.grid(row=0, column=1, sticky=N+S)
entry_frame.pack(side=TOP, anchor=NW)

# Now execute the dialog
dialog.activate()
return

def ListFiles():
global dialog
dialog = LoadFileDialog(dialog.parent)
file = dialog.go("")
print file

def DialogAction(button_name):
global dialog
if button_name == 'Cancel':
dialog.deactivate()
elif button_name == 'Ok':
dialog.deactivate()
return

# Test it by parsing a prm file
if __name__ == '__main__':
tk_root = Tk()
foo = CreatePMWDialog(tk_root)
tk_root.mainloop()
Problem when using FileDialog from PMW Dialog. Quick fixes? [ In reply to ]
Chad Netzer wrote:

> Before I pursue this further, can anyone tell me if it is even worth
> trying? It is nice to use the canned FileDialog, but its implementation
> is somewhat crufty (IMHO) and I can probably work around it in other
> ways (Creating my own FileDialog from PMW widgets is a nice possibility.

Yet another reply to my own post. After this last message, I said "What
the hell",
and converted the FileDialog.py from the standard Python Tkinter
distribution
to use PMW dialogs. It took about half an hour :) Damn I love this
language!

I'm on a bit of a deadline so I hadn't thought to devote time to this
originally, but
it was quick, easy, the result looks nicer, and it WORKS. I'll post it
somewhere
sometime later, and hopefully reorganize it to fit the general PMW scheme
of
organizing objects, then submit it for inclusion.

Anyway, I'd still like to hear an answer to my original query, but for now,
it is
academic (for me).

Cheers,
Chad
chad@vision.arc.nasa.gov
Problem when using FileDialog from PMW Dialog. Quick fixes? [ In reply to ]
What you are trying to do is common practice.

If this code snippet is accurate, the problem is in your use of global
dialog. In CreatePMWDialog you assign dialog to the Pmw.Dialog
then in ListFiles, you chage the binding of the global dialog variable
to point to the FileDialog. So your subsequent call to DialogAction is
trying to act on a non-existing widget.

If you changeListFiles to the following, everything works fine.

def ListFiles():
global dialog
dlg = LoadFileDialog(dialog.parent)
file = dlg.go("")
print file

You might want to look at using tkFileDialog instead. It brings up the
standard file dialogs, presenting a more familiar interface to Windows
users.

check out http>//www.pythonware.com/library/tkinter/introduction/ for
more info on how to use. (Sorry I can't give you more info, I can't
get through to pythonware just now)

Good Luck!
Robert Roy




On Thu, 12 Aug 1999 01:01:21 +0000, Chad Netzer
<chad@vision.arc.nasa.gov> wrote:

>I have an application where I open a Python Mega Widget dialog (local,
>not global) and within it I later open a FileDialog() for file selection
>(Don't ask, why, but calling a nested dialog makes sense for the app.)
>In any case, it doesn't work. When the FileDialog exits, the Pmw Dialog
>cannot get focus back (even with some code to try to force it.)
>
>Before I pursue this further, can anyone tell me if it is even worth
>trying? It is nice to use the canned FileDialog, but its implementation
>is somewhat crufty (IMHO) and I can probably work around it in other
>ways (Creating my own FileDialog from PMW widgets is a nice possibility.
>) But if there is a quick fix, I'd like to hear it.
>
>Below you'll find some psuedo-code to show roughly what I'm doing. Run
>the code, click on the "List" button to activate the FileDialog, then
>exit the FileDialog. Hitting the PMW Dialog "Cancel" button doesn't
>work (as it does if you never open the FileDialog). Pardon the rough
>code :) You will probably have to kill the process if you trigger
>the problem, so be warned.
>
>Chad Netzer
>chad@vision.arc.nasa.gov
>
>------------------------------ [ Cut Here ]
>------------------------------
>from Tkinter import *
>import Pmw
>from FileDialog import LoadFileDialog
>
>def CreatePMWDialog(parent):
> global dialog
> dialog = Pmw.Dialog(parent,
> buttons = ('OK', 'Cancel'),
> defaultbutton = 'OK',
> title = 'A test: The FileDialog will steal focus
>forever',
> command = DialogAction)
> dialog.parent = parent
>
> interior = dialog.interior()
> entry_frame = Frame(interior)
> list_but = Button(entry_frame, text="List", command=ListFiles)
>
> list_but.grid(row=0, column=1, sticky=N+S)
> entry_frame.pack(side=TOP, anchor=NW)
>
> # Now execute the dialog
> dialog.activate()
> return
>
>def ListFiles():
> global dialog
> dialog = LoadFileDialog(dialog.parent)
> file = dialog.go("")
> print file
>
>def DialogAction(button_name):
> global dialog
> if button_name == 'Cancel':
> dialog.deactivate()
> elif button_name == 'Ok':
> dialog.deactivate()
> return
>
># Test it by parsing a prm file
>if __name__ == '__main__':
> tk_root = Tk()
> foo = CreatePMWDialog(tk_root)
> tk_root.mainloop()
>
>
Problem when using FileDialog from PMW Dialog. Quick fixes? [ In reply to ]
"Robert J. Roy" wrote:

> If this code snippet is accurate

It isn't, in my haste to make an example I accidentally reused the dialog
variable instead of making a separate one for the nested dialog. I noticed
it a while after I posted, when I was creating a related demo for Greg
McFarlane. However, due to chance, it doesn't affect the demonstration,
because the binding and callbacks have been registered by the time dialog
gets remapped. At least on my system (Linux), the same result occurs
with either my original code, or your revised code (ie. the Pmw.Dialog
window does not respond to button selections anymore) I did some
more tests, and found this behavior to be somewhat common. (Greg
pointed me to PmwFileDialog in the Pmw contribs, and I was able to
get similar behavior by triggering modal dialogs to popup during its
activation.)


> You might want to look at using tkFileDialog instead. It brings up the
> standard file dialogs, presenting a more familiar interface to Windows
> users.

Ah yes, I have used it in the past, and when I went looking for it, I forgot
that it isn't in the Tkinter module. Having the native look would be
useful... Let me test it out [5-10 min later] Yes, it works fine nesting
with a Pmw widget. I'd forgotten how ugly that requestor looks on Unix,
though. :) Cheers, I'll keep it in as an option for now.

Thanks for the help.

Chad
chad@vision.arc.nasa.gov