I don't know what to do with this xlib error I get from the code below (please paste it into the python console and see it for yourself).

I got some advice from some guy, but I am a newbie to wxpython so I don't know how to follow it and it will take a long time until I will become capable to do it. Since it may be much easier for you to do it, please somebody modify my code for me. This will be a much faster way for me to learn about threads and wxpython, too.

Note that this is just a simplified sample of my code, so don't try to change its design *completely* (just because this sample looks stupid and "useless anyway"). Using the same wxpython function in separate threads is vital. Think of it as a function that acts as a python implementation of zenity or (k)dialog. I just need to call this "(k)dialog"-like window function from many functions in many separate threads. However, no problem if I will need some extra code (i.e. outside the function) in the main body of the script. I just want as much as possible of the wx to be confined to a function.

This helps me keep the GUI and the code as separated as possible in my script. I am a console guy and prefer to write "scripts (sometimes with some GUI features)", not gripts.


#!/bin/python
import wx, threading; from wx.html import HtmlWindow
def wxwin():
class HtmlDialog(wx.MessageDialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.htmlwindow = HtmlWindow(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.clbutton = wx.Button(self, wx.ID_CLOSE)
self.sizer.Add(self.htmlwindow)
self.sizer.Add(self.clbutton)
self.htmlwindow.SetPage('message')
self.Bind(wx.EVT_BUTTON, self.function, self.clbutton)
def function(self, event): self.Close()
if __name__ == '__main__':
app = wx.PySimpleApp()
dlg = HtmlDialog(None)
print str(dlg.ShowModal())

def thrd(): wxwin()

while True: wxwin(); threading.Thread(target=thrd).start()

THIS IS THE HELP I GOT (may be useful for you too):
you can't do wx things from more than one thread (except for things like wx.PostEvent or wx.CallAfter) and you can't have more than one wx.App object at a time.

ok, have only one thread for the UI that initializes wx and starts the MainLoop. In the other threads when you need to call a dialog function break it into two parts, one that just sends a message to the UI thread and waits for the response, and the other to catch the message in the UI thread that creates and shows the dialog, calls ShowModal and then send a message back to the waiting thread with the results.


I think I should use queue and PostEvent, but I don't know how.

Sorry. Here's the code as an attachment (correctly indented). Now you can simply paste it into your python console.

Attachments
#!/bin/python
import wx, threading; from wx.html import HtmlWindow
def wxwin():
    class HtmlDialog(wx.MessageDialog):
        def __init__(self, parent):
            wx.Dialog.__init__(self, parent)
            self.htmlwindow = HtmlWindow(self)
            self.sizer = wx.BoxSizer(wx.VERTICAL)
            self.clbutton = wx.Button(self, wx.ID_CLOSE)
            self.sizer.Add(self.htmlwindow)
            self.sizer.Add(self.clbutton)
            self.htmlwindow.SetPage('message')
            self.Bind(wx.EVT_BUTTON, self.function, self.clbutton)
        def function(self, event): self.Close()
    if __name__ == '__main__':
        app = wx.PySimpleApp()
        dlg = HtmlDialog(None)
        print str(dlg.ShowModal())

def thrd(): wxwin()

while True: wxwin(); threading.Thread(target=thrd).start()
This article has been dead for over six months. Start a new discussion instead.