From patrick.dunnigan at cloudsidekick.com Fri Aug 3 19:51:35 2012 From: patrick.dunnigan at cloudsidekick.com (Patrick Dunnigan) Date: Fri, 3 Aug 2012 13:51:35 -0400 Subject: [Tkinter-discuss] tkinter error handling when tcl calls python functions Message-ID: Hi all, Scenario: I am transitioning a mature tcl package to python. As in interim step in the long process, I will use tkinter in a python script to call the tcl code. Since I have some common functionality already written in python, I am using the createcommand function to expose this to tcl. As a side note, there is no tk in this software, only using the tcl piece. Another side note: the useTk=0 argument appears to have no effect. Everything works nicely. However I have found that when the tcl code calls a python function and an error occurs in the python, the python error is not included in the tcl error stack. I've spent hours hacking away trying to figure it out, but now it's time to turn to the community. Here's a boiled down script that can be used to reproduce and illustrate: example 1, this is the best message I can get out of tcl, but doesn't include the error that occurs in the "hello" function: #!/usr/bin/env python from Tkinter import Tcl def hello(): # the error will occur on the following line ---> print z return tcl = Tcl() tcl.createcommand('hello', hello) try: tcl.eval('hello') except Exception as e: print tcl.getvar(name="errorInfo") raise e ./test.py while executing "hello" Traceback (most recent call last): File "./test.py", line 15, in raise e _tkinter.TclError Example 2, this is what I would expect the tcl error message to include: $ python Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print z Traceback (most recent call last): File "", line 1, in NameError: name 'z' is not defined The ideal error message would be something like (or with more traceback nesting if in a tcl procedure): Traceback (most recent call last): File "", line 1, in NameError: name 'z' is not defined while executing "hello" Traceback (most recent call last): File "./test.py", line 15, in raise e _tkinter.TclError Thanks, Patrick From list at qtrac.plus.com Wed Aug 29 19:01:24 2012 From: list at qtrac.plus.com (Mark Summerfield) Date: Wed, 29 Aug 2012 18:01:24 +0100 Subject: [Tkinter-discuss] tkinter and multiprocessing Message-ID: <20120829180124.35c4f430@dino> Hi, I'm trying to create a Tkinter application that uses multiprocessing in a separate module to do some work. In the Tkinter part I have this method of my Window ttk.Frame subclass: def report(self, future): with StatusLock: # Serialize calls to Window.report() if future.exception() is None: result = future.result() print(result) # TODO why does this lock the application? self.statusText.set(result.name) self.master.update() # Make sure the GUI refreshes StatusLock is a multiprocessing.Lock. self.statusText is a tk.StringVar. This function is called by a multiprocessing Future: future = executor.submit(action, arg1, arg2) future.add_done_callback(report) When I run the program with the last two lines of report() commented out it correctly prints each result on the console. But with the last two lines uncommented it prints one result and then the whole application locks at the self.statusText.set() call. According to the multiprocessing docs the callback is executed in the thread of the function it is passed -- so in this case in the GUI thread which is what is wanted. Any suggestions/help welcome:-) Thanks! -- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "Advanced Qt Programming" - ISBN 0321635906 http://www.qtrac.eu/aqpbook.html From list at qtrac.plus.com Thu Aug 30 09:05:38 2012 From: list at qtrac.plus.com (Mark Summerfield) Date: Thu, 30 Aug 2012 08:05:38 +0100 Subject: [Tkinter-discuss] tkinter and multiprocessing In-Reply-To: <20120829180124.35c4f430@dino> References: <20120829180124.35c4f430@dino> Message-ID: <20120830080538.2a9b40d1@dino> Hi, Silly me! I forgot to do the multiprocessing in a separate thread: now it works:-) On Wed, 29 Aug 2012 18:01:24 +0100 Mark Summerfield wrote: > Hi, > > I'm trying to create a Tkinter application that uses multiprocessing in > a separate module to do some work. > > In the Tkinter part I have this method of my Window ttk.Frame subclass: > > def report(self, future): > with StatusLock: # Serialize calls to Window.report() > if future.exception() is None: > result = future.result() > print(result) > # TODO why does this lock the application? > self.statusText.set(result.name) > self.master.update() # Make sure the GUI refreshes > > StatusLock is a multiprocessing.Lock. > self.statusText is a tk.StringVar. > > This function is called by a multiprocessing Future: > > future = executor.submit(action, arg1, arg2) > future.add_done_callback(report) > > When I run the program with the last two lines of report() commented out > it correctly prints each result on the console. But with the last two > lines uncommented it prints one result and then the whole application > locks at the self.statusText.set() call. > > According to the multiprocessing docs the callback is executed in the > thread of the function it is passed -- so in this case in the GUI thread > which is what is wanted. > > Any suggestions/help welcome:-) > > Thanks! -- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "Programming in Go" - ISBN 0321774639 http://www.qtrac.eu/gobook.html