Search code examples
python-3.xwxpythontwistedtwisted.internet

reactor.registerWxApp(App) giving Attribute error 'SelectReactor' object has no attribute 'registerWxApp'


I am currently getting the error:

reactor.registerWxApp(app)
AttributeError: 'SelectReactor' object has no attribute 'registerWxApp'

I can't seem to add the app to the reactor since registerWxApp is not being recognized? Also if i don't catch the wxreactor it raises the error that wxreactor is already installed.

if __name__ == '__main__':
    import wx
    from twisted.internet import wxreactor
    try:
        wxreactor.install()
    except:
        print('already installed')
    # import t.i.reactor only after installing wxreactor:
    from twisted.internet import reactor


STREAM_URL = url

print(STREAM_URL)

factory = WebSocketClientFactory(STREAM_URL)
factory.protocol = MyClientProtocol

print('hello')

print('hi')


app = wx.App(False)
app._factory = factory
app._frame= testapi(None)
app._frame.Show()
reactor.registerWxApp(app)
print(reactor)
reactor.run()

app.MainLoop()

Solution

  • You've made an assumption here:

    from twisted.internet import wxreactor
    try:
        wxreactor.install()
    except:
        print('already installed')
    

    The assumption is that the only reason wxreactor.install() could raise an exception is that wxreactor is already installed. I expect the assumption is invalid.

    Try to avoid writing except: in your Python programs unless you plan to include a bare raise in the block to re-raise whatever exception you're handling (and even so, consider using finally for this instead).

    When you catch all exceptions you risk catching exceptions your code can't actually handle correctly. In this case, perhaps there is some other problem installing wxreactor. If so, then you're probably getting the default reactor when code proceeds to import twisted.internet.reactor after this attempt.

    The exception you reported:

    AttributeError: 'SelectReactor' object has no attribute 'registerWxApp'
    

    means you do not have wxreactor installed. You have SelectReactor installed. You cannot register a wx app with select reactor.

    You need to diagnose the cause of wxreactor.install() failing and you cannot do that by squashing and ignoring all exceptions from that call.