[Pythonmac-SIG] py2app bug with argv_emulation=True

Kevin Walzer kw at codebykevin.com
Sun Jun 3 03:14:21 CEST 2012


You may need to initialize the create command bits later in the app to avoid the open doc event getting swallowed up during startup...

Sent from my iPhone

On May 31, 2012, at 7:47 AM, "Michael O'Donnell" <micko at wagsoft.com> wrote:

> Hi Kevin, Ronald,
> 
>   I must be missing something. I have inserted the discussed
> code into a test file (test.py) as follows:
> 
> ----------------------
> import sys
> from Tkinter import *
> import tkMessageBox
> 
> def doOpenFile(*args):
>     for arg in args:
>         tkMessageBox._show(message=str(arg), type="ok", icon=tkMessageBox.WARNING)
> 
> tk = Tk()
> tk.createcommand("::tk::mac::OpenDocument", doOpenFile)
> fr=Frame(tk, height=400, width=400, bg="red")
> fr.pack()
> tk.mainloop()
> -----------------------
> 
> Then I package this using py2app, specifying that it should launch
> whenever a .ctpx file is double clicked:
> 
> -------
> from setuptools import setup
> import sys
> OPTIONS={'py2app': {'argv_emulation': True, 'resources': [],
>                     'plist': dict(CFBundleDocumentTypes = [dict(
>                             CFBundleTypeExtensions = ["ctpx"],
>                             CFBundleTypeName = "Test File",
>                             CFBundleTypeRole = "Editor")])}}
> sys.argv=[sys.argv[0]]
> sys.argv.append("py2app")
> setup(
>     app=['test.py'],
>     data_files=[],
>     options=OPTIONS,
>     setup_requires=['py2app'])
> ------
> 
> Running this creates an app: Test.app.
> 
> If I then click on a file with a .ctpx extension, the application
> opens, but doOpenFile is not called (not message pops up).
> If I click on the .ctpx file again (while the app is open) the
> message DOES appear.
> 
> Now, this is all as I would expect, because my  call of
> tk.createcommand happens only AFTER my application
> launches, and any openDocument event was probably
> handled BEFORE the first line of my python code is
> executed.
> 
> So none of this is any help to me. My users expect to
> double click on a .ctpx document to launch the application
> AND open that document for editing.
> 
> Have I missed something?
> 
> Mick
> 
> 
> 
> On Wed, May 30, 2012 at 8:29 PM, Kevin Walzer <kw at codebykevin.com> wrote:
> 
> 
> IDLE can be opened by double clicking on python files, the code I linked
> to should be responsible for implementing this. Also, the argv_emulation
> code uses the openDocument event to do its work (argv_emulation basicly
> runs a Carbon event loop until it has received some openDocument events
> or until a timeout occurs).
> 
> Also:
> 
> *smacks head*
> 
> Mick, I didn't realize that you were trying to have your app respond to a file being dropped on the app icon. Ronald is right, argv_emulation is not necessary for this.
> 
> If the code sample Ronald pointed to isn't quite clear, let me explain it a bit more:
> 
> Tk on the Mac supports all of the basic Apple events (app launch, file open, etc.) out of the box -- stub commands are included in the code that you can fill out to trigger an action in response to one of the supported events.
> 
> In the case of opening a file by dropping it on the app icon, or double-clicking it, the relevant Tk command is "tk::mac::OpenDocument" . When this command is defined in Tk, the code contained in the command will be executed when an "odoc" ("open document") event is received.
> 
> To access this functionality from Tkinter, you should use Tkinter's createcommand() function. The "createcommand" functionality allows a Python function/method to be mapped to a Tk command. Hence, in the IDLE example, you have:
> 
> root.createcommand("::tk::mac::OpenDocument", doOpenFile)
> 
> which maps the following code to the "tk::mac::OpenDocument" command:
> 
>   def doOpenFile(*args):
>            for fn in args:
>                 flist.open(fn)
> 
> 
> Tk's support for this stuff on the Mac, out of the box, is actually quite rich, but it's been poorly documented until quite recently. I contributed documentation on all these commands to the Tk man pages:
> 
> http://www.tcl.tk/man/tcl8.6/TkCmd/tk_mac.htm
> 
> The commands are documented for Tk 8.6, but they are all present in the Cocoa-based version of Tk 8.5 from ActiveState as well, so you may find some useful things to look at here.
> 
> Hope this helps,
> Kevin
> 
> -- 
> Kevin Walzer
> Code by Kevin
> http://www.codebykevin.com
> _______________________________________________
> Pythonmac-SIG maillist  -  Pythonmac-SIG at python.org
> http://mail.python.org/mailman/listinfo/pythonmac-sig
> unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG
> 
> 
> _______________________________________________________
> Unlimited Disk, Data Transfer, PHP/MySQL Domain Hosting
>             http://www.doteasy.com
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythonmac-sig/attachments/20120602/2e3cd8ca/attachment.html>


More information about the Pythonmac-SIG mailing list