Opened 9 years ago

Closed 9 years ago

#12225 closed defect (invalid)

Mac OS X only: wx.TextCtrl.AppendText and .WriteText throw exception if executed from a thread

Reported by: eysispeisi Owned by:
Priority: normal Milestone:
Component: GUI-generic Version: 2.8.x
Keywords: TextCtrl AppendText WriteText Mac threading Cc:
Blocked By: Blocking:
Patch: no

Description

This issue is only a problem on mac machines, i have tested this on two different macs each running mac os x 10.5.8 and the other 10.6.x, the issue occurred in both instances.
python25-wx2.8 was used on the 10.5.8 OS and python26-wx?? was used on 10.6.x

This will work just fine on Windows machines.

The issue is that if the wx.TextCtrl.AppendText or wx.TextCtrl.WriteText functions are called from within a thread, an exception is thrown and the GUI gets locked up.

Code:
# ---------------------------------------------------------------------------------------
import wx
import threading, sys, traceback

class Frame(wx.Frame):

def init(self, parent=None):

wx.Frame.init(self, parent=None)
sizer = wx.BoxSizer(wx.VERTICAL)

self.text = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.button = wx.Button(self, label='display a line of text')

sizer.Add(self.text, 1, wx.EXPAND)
sizer.Add(self.button)
self.SetSizer(sizer)

self.Bind(wx.EVT_BUTTON, self.OnButtonClick, self.button)

def OnButtonClick(self, evt):

lines = [

'a line of text\n',
'anohter line of text\n',
]

for line in lines:

threading.Thread(target=self.DisplayText, args=(line,)).start()

def DisplayText(self, line):

try:

self.text.AppendText(line)

except:

sys.stdout.write(traceback.format_exc())

class App(wx.App):

def OnInit(self):

frame = Frame(self)
frame.Show()
frame.Center()
return True

app = App()
app.MainLoop()
# ---------------------------------------------------------------------------------------

when the "display a line of text" button is pressed it will throw the following exception:

Traceback (most recent call last):

File "test.py", line 24, in MultipleTexts

self.text.AppendText('a line of text\n')

File "/BinaryCache/wxWidgets/wxWidgets-11~57/Root/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/wx-2.8-mac-unicode/wx/_controls.py", line 1797, in AppendText

PyAssertionError: C++ assertion "gs_nWaitingForGui > 0" failed at ../src/mac/carbon/thread.cpp(1727) in wxMutexGuiLeave(): calling wxMutexGuiLeave() without entering it first?

Change History (1)

comment:1 Changed 9 years ago by vadz

  • Resolution set to invalid
  • Status changed from new to closed

We don't support using GUI elements from other threads. You need to post a message to the main thread asking it to update it.

Note: See TracTickets for help on using tickets.