473,382 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Crashing Python.exe with TkInter


I've run into a problem with Python/TkInter crashing, with an
attempt to read illegal addresses, on Win on Win2000 and WinXP.

With some web-searching, I found that people say not to
manipulate TkInter from multiple threads, except that the call
event_generate() is safe. I assume the call adds an event to
Tk's queue, and later the thread running mainloop will() pick up
the event.

I tried to write a general-purpose thread-runner. In the code
below, 'thread-it' runs a given function in a new thread, saves
the result, and triggers event_generate(). When the mainloop
handles the event, it calls a second given function with the
saved result.

The code below *seems* to work. It requires only the current
Python distribution. To make it usually crash, un-comment the
'print' statement in add_to_listbox(). That's all.

With no prints, it works. Adding prints in various places
causes it usually to crash with an unreadable-memory error from
the OS. I get the same behavior on the command-line and in
Idle. Occasionally, it prints something like:

TclExecuteByteCode: abnormal return at pc 45: stack top 39 < entry
stack top 59
TclExecuteByteCode execution failure: end stack top < start stack top

which is usually also accompanied by the read addressing error.

I'm sure I've previously been able to print from multiple Python
threads. Can anyone tell what is going on?

from Tkinter import *
import thread
import time

class Application(Frame):

def __init__(self):
Frame.__init__(self, None)
self.event_name_cntr = 0
self.pack()
self.listbox = Listbox(self)
self.listbox.pack(side=TOP)

def add_to_listbox(self, msg):
self.listbox.insert(END, msg)
# print "Adding", msg

def thread_it(self, finish_func, asyc_func, args=()):
""" Create a thread that calls asyc_func(*args), then trigger
an even with event_generate(). When the event loop handles
the event, call finish_func with the return value of asyc_func.
"""
event_name = "<<j7A5x0VjqZ3b_%x>>" % self.event_name_cntr
self.event_name_cntr += 1
result = [None]
def _on_finish(_):
self.unbind(event_name)
finish_func(result[0])
def do():
result[0] = asyc_func(*args)
self.event_generate(event_name)
self.bind(event_name, _on_finish)
thread.start_new_thread(do, ())

app = Application()

def fin(s):
""" When the async function returns, we'll add its output
to the list box.
"""
app.add_to_listbox(s)

def async(n):
""" In the threads, we'll just sleep then return a string.
"""
time.sleep(2.0)
return "Got %d asyncronously." % n

for i in range(5):
# Run a few threads
app.thread_it(fin, async, [i])

app.mainloop()

Thanks,
--
--Bryan
Jul 18 '05 #1
0 1292

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
14
by: Java and Swing | last post by:
static PyObject *wrap_doStuff(PyObject *self, PyObject *args) { // this will store the result in a Python object PyObject *finalResult; // get arguments from Python char *result = 0; char *in=...
1
by: John Chambers | last post by:
Sp my latest adventure is attempting to use python's Tkinter module on a few machines. On my PB (OSX 10.3.9), I got the following confusing results: /Users/jc: python Python 2.3 (#1, Sep 13...
44
by: jiang.haiyun | last post by:
Now i began to learn GUI programming. There are so many choices of GUI in the python world, wxPython, pyGTK, PyQT, Tkinter, .etc, it's difficult for a novice to decide, however. Can you draw a...
40
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
I'm really annoyed at Python - and not for the reasons already mentioned on this list. Everyone know that programming is supposed to be a dark art, nearly impossible to learn. Computer code is...
13
by: Daniel Fetchinson | last post by:
Was looking at PEP 3108, http://www.python.org/dev/peps/pep-3108/ , Is it just me or others also think that it would be a major loss to remove tkinter from the python core? PEP 3108 starts off...
8
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to...
2
by: Dudeja, Rajat | last post by:
Hi, So, now I've finally started using Eclipse and PyDev as an IDE for my GUI Application. I just wrote some sample programs as an hands on. Now I would like to take up Tkinter. I'm using...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.