Hello everyone,
I am having strange dialog behavior again (see thread http://aspn.activestate.com/ASPN/Mail/Message/wxpython-users/3657891)
If I create a dialog, hide it, and then call a wx.MessageDialog, it works the first time and then it no longer works. I have included an example below, to test it, listen on TCP port 4321 (under linux I use netcat):
nc -l -v -p 4321
and run the example, then in netcat press a key (a letter or number) and press enter, it should popup the wx.MessageDialog the first time and then it never does again, it gets lost in the wx.MessageDialog code. I originally had a Twisted example, (I found this while using Twisted & wx integration) but I wrote this pure (as in not having external libs) python example. Is this a bug (probably like the one I had before http://trac.wxwidgets.org/ticket/9909) or am I doing something strange? I also included my Twisted example at the very bottom
Thank you,
Gabriel
################### Pure python begin #######################
# -*- coding: utf-8 -*-
import wx, socket, sys
class TestDialog(wx.Frame):
def __init__(self, parent=None, id=wx.ID_ANY, title="Test",
pos=wx.DefaultPosition, size=(300, 175), *args, **kwargs):
wx.Frame.__init__(self, parent, id, title, pos, size, *args, **kwargs)
panel = wx.Panel(self, wx.ID_ANY)
wx.Button(panel, wx.ID_OK, " OK ", pos=(100, 115), size=wx.DefaultSize).SetDefault()
self.Bind(wx.EVT_BUTTON, self.onOk, id=wx.ID_OK)
self.srv = None
self.Bind(wx.EVT_CLOSE, self.onOk)
def onOk(self, error):
"""
Called when a timeout error arrives
@param error: the error msg
@type error: C{str}
"""
dlg = wx.MessageDialog(None, "Error : test", "Error", wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
wx.CallAfter(dlg.Destroy)
class TestApp(wx.App):
def __init__(self, *args, **kwargs):
wx.App.__init__(self, False, clearSigInt=False)
def OnInit(self):
self.gui = TestDialog()
self.gui.Center(wx.BOTH)
self.gui.Show(False)
self.SetTopWindow(self.gui)
return True
def startedConnecting(self, connector):
print 'Started to connect.'
def buildProtocol(self, addr):
print 'Connected.'
p = TestService()
p.factory = self
return p
def clientConnectionLost(self, connector, reason):
print 'Lost connection. Reason:', reason
def clientConnectionFailed(self, connector, reason):
print 'Connection failed. Reason:', reason
reactor.callLater(0, reactor.stop)
def listen(app):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(1)
try:
sock.connect(("localhost", 4321))
except socket.error, msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(2)
while True:
data = sock.recv(1024)
print "got data"
app.gui.onOk(None)
if(__name__ == "__main__"):
app = TestApp()
listen(app)
################### Pure python end #######################
################### Twisted begin #######################
# -*- coding: utf-8 -*-
import wx
from twisted.internet import wxreactor
try:
wxreactor.install()
except:
pass
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ClientFactory
class TestDialog(wx.Frame):
def __init__(self, parent=None, id=wx.ID_ANY, title="Test",
pos=wx.DefaultPosition, size=(300, 175), *args, **kwargs):
wx.Frame.__init__(self, parent, id, title, pos, size, *args, **kwargs)
panel = wx.Panel(self, wx.ID_ANY)
wx.Button(panel, wx.ID_OK, " OK ", pos=(100, 115), size=wx.DefaultSize).SetDefault()
self.Bind(wx.EVT_BUTTON, self.onOk, id=wx.ID_OK)
self.srv = None
self.Bind(wx.EVT_CLOSE, self.onOk)
def onOk(self, error):
dlg = wx.MessageDialog(None, "Error : test", "Error", wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy
class TestFactory(ClientFactory, wx.App):
def __init__(self, *args, **kwargs):
wx.App.__init__(self, False, clearSigInt=False)
def OnInit(self):
self.gui = TestDialog()
self.gui.Center(wx.BOTH)
self.gui.Show(False)
self.SetTopWindow(self.gui)
return True
def startedConnecting(self, connector):
print 'Started to connect.'
def buildProtocol(self, addr):
print 'Connected.'
p = TestService()
p.factory = self
return p
def clientConnectionLost(self, connector, reason):
print 'Lost connection. Reason:', reason
def clientConnectionFailed(self, connector, reason):
print 'Connection failed. Reason:', reason
reactor.callLater(0, reactor.stop)
class TestService(Protocol):
def dataReceived(self, data):
print "Received: %s" % str(data)
self.factory.gui.onOk(None)
def connectionMade(self):
print "client connected"
if(__name__ == "__main__"):
f = TestFactory()
reactor.connectTCP("localhost", 4321, f)
reactor.registerWxApp(f)
reactor.run()
################### Twisted end #######################