File Dialog

246 views
Skip to first unread message

Daniel

unread,
Nov 29, 2009, 11:08:46 PM11/29/09
to pyglet-users
Any tips on getting a file dialog to work with pyglet? I naively
tried:

root = Tkinter.Tk()
root.withdraw()
filename = tkFileDialog.askopenfilename()

but this causes an uncaught exception once the dialog is closed either
by canceling or selecting a file (the exception happens some time
after it passes back the path string, I can successfully print the
variable 'filename', but then it immediately crashes with a signal 11
or signal 10 if I do not attempt to read the variable 'filename')

I know kytten offers some rudimentary functionality for this, but
would prefer to avoid that as I am already using glydget.

Any help, tips, or points in the right direction will be appreciated.

Thanks,
Dan

Richard Jones

unread,
Nov 29, 2009, 11:17:33 PM11/29/09
to pyglet...@googlegroups.com
On 30/11/2009, at 3:08 PM, Daniel wrote:
> Any tips on getting a file dialog to work with pyglet? I naively
> tried:
>
> root = Tkinter.Tk()
> root.withdraw()
> filename = tkFileDialog.askopenfilename()

I use this in a pyglet app with no problems. What's the exception you get?

Do you "root.destroy()" when the file dialog is finished with?


Richard

Daniel

unread,
Nov 29, 2009, 11:22:51 PM11/29/09
to pyglet-users
Yes, I do root.destroy() after the dialog is complete. I just get:

Program terminated by uncaught signal #10 after 5.37 seconds.

if I do not try to access the variable, but if I do try to access the
variable it is signal 11. I'm on a mac running snow leopard if that
may be part of the issue.

Dan

Daniel

unread,
Nov 29, 2009, 11:26:34 PM11/29/09
to pyglet-users
This may be a problem with glydget, I moved some code around and it
works elsewhere, but not if I'm calling it from a glydget button
action.

I'll look into it some more....

Daniel

unread,
Nov 29, 2009, 11:39:03 PM11/29/09
to pyglet-users
Now I am not convinced that it has to do with glydget. If I set it up
to open the file dialog box after pressing a key I still get the
error. I also occasionally get errors about the autorelease pool:

2009-11-29 20:37:33.280 Python[25903:d0f] *** __NSAutoreleaseNoPool():
Object 0xa069ec68 of class NSCFString autoreleased with no pool in
place - just leaking

Steve

unread,
Nov 30, 2009, 12:18:20 AM11/30/09
to pyglet-users
I wrote some code a year or so ago that will open a native dialog on
most OSes. No dependencies.

http://github.com/irskep/pygletlibs/blob/master/dialogs.py

Daniel

unread,
Nov 30, 2009, 11:03:21 AM11/30/09
to pyglet-users
For those that are curious, this appears to be a problem with Snow
Leopord (running with the prefer 32bit workaround). The exact program
works fine on another mac using Leopord. The error occurs when you
exit out of a tkinter file dialog either by pressing the OK or Cancel
buttons, and only occurs if the pyglet event loop is running (you can
put the open dialog code before pyglet.app.run() and it runs fine).

Dan

Tristam MacDonald

unread,
Nov 30, 2009, 11:26:58 AM11/30/09
to pyglet...@googlegroups.com
On Mon, Nov 30, 2009 at 11:03 AM, Daniel <brid...@gmail.com> wrote:
For those that are curious, this appears to be a problem with Snow
Leopord (running with the prefer 32bit workaround).  The exact program
works fine on another mac using Leopord.  The error occurs when you
exit out of a tkinter file dialog either by pressing the OK or Cancel
buttons, and only occurs if the pyglet event loop is running (you can
put the open dialog code before pyglet.app.run() and it runs fine).

If you are able to put together a minimal sample, I can give it a go on my Snow Leopard machines, and see if I can find the conflict.

--
Tristam MacDonald
http://swiftcoder.wordpress.com/

Daniel

unread,
Nov 30, 2009, 12:03:31 PM11/30/09
to pyglet-users
Here you go:

#!/usr/bin/env python
# encoding: utf-8

import pyglet
from pyglet.window import key

import Tkinter, tkFileDialog

window = pyglet.window.Window(width=1024, height=768, caption='Dialog
Test')

@window.event
def on_draw():
window.clear()

@window.event
def on_key_press(symbol, modifiers):
if symbol == key.O:
root = Tkinter.Tk()
root.withdraw()
filename = tkFileDialog.askopenfilename()
root.destroy()
print filename

if __name__ == '__main__':
pyglet.app.run()






On Nov 30, 8:26 am, Tristam MacDonald <swiftco...@gmail.com> wrote:

Tristam MacDonald

unread,
Nov 30, 2009, 12:35:20 PM11/30/09
to pyglet...@googlegroups.com
On Mon, Nov 30, 2009 at 12:03 PM, Daniel <brid...@gmail.com> wrote:
Here you go:

#!/usr/bin/env python
# encoding: utf-8

import pyglet
from pyglet.window import key

import Tkinter, tkFileDialog

window = pyglet.window.Window(width=1024, height=768, caption='Dialog
Test')

@window.event
def on_draw():
   window.clear()

@window.event
def on_key_press(symbol, modifiers):
   if symbol == key.O:
       root = Tkinter.Tk()
       root.withdraw()
       filename = tkFileDialog.askopenfilename()
       root.destroy()
       print filename

if __name__ == '__main__':
   pyglet.app.run()

Looks like a problem with the Pyglet/Tkinter combo on Snow Leopard. Something to do with Pyglet's Carbon run loop interacting badly with Tkinter's Cocoa run loop.

I would hazard a guess that it is not possible to work around that issue at the moment.

Greg Ewing

unread,
Nov 30, 2009, 5:24:19 PM11/30/09
to pyglet...@googlegroups.com
Daniel wrote:
> I also occasionally get errors about the autorelease pool:
>
> 2009-11-29 20:37:33.280 Python[25903:d0f] *** __NSAutoreleaseNoPool():
> Object 0xa069ec68 of class NSCFString autoreleased with no pool in
> place - just leaking

You might like to try using Cocoa's file dialogs via PyObjC.

I once tried to use Tkinter from inside a Cocoa-based
application, and ended up with crashes. It seemed like
Cocoa and Tk were allergic to each other somehow.

--
Greg

Richard Jones

unread,
Nov 30, 2009, 6:00:17 PM11/30/09
to pyglet...@googlegroups.com
This does not crash on my Snow Leopard. To solve a bunch of other Tkinter-related and build-related issues I have installed the latest ActiveTcl (8.5) and ActivePython (2.6.4.8) on my machine.

I did have to patch one bug in pyglet.app.base but it was event loop timing related and not a segfault. That fix is in SVN.


Richard
Reply all
Reply to author
Forward
0 new messages