Pregunta

I am currently developing an GUI app with python v2.7 and wxPython v3.0 on windows 7 OS. I am using pubsub module for sending the information to my main GUI thread to update my GUI. I am using wx.CallAfter() to send the messages to main GUI loop.

Problem: In my programm there is an instance that I need to send two lists using wx.CallAfter() like shown below:

wx.CallAfter(pub.sendMessage, 'Update', ListA, ListB)

I get following error:

sendMessage() takes at most 3 arguments (4 given)

Any work around for this without modifying my method that is receiving this messages?

wx.CallAfter(pub.sendMessage, 'Update', ListA) works with charm.

Thank you for your time.

Answer: I was using following imports

from wx.lib.pubsub import setuparg1
from wx.lib.pubsub import pub

I should use following which solved my problem:

from wx.lib.pubsub import setupkwargs
from wx.lib.pubsub import pub
¿Fue útil?

Solución

You can only send messages by keyword value, so you have to do this:

from wx.lib.pubsub import pub 
...
wx.CallAfter(pub.sendMessage, 'Update', arg1 = ListA, arg2 = ListB)

The arg1 and arg2 must be the same as the listener arguments (so all listeners of given topic ('Update'), and all senders for that topic, must use the same argument names; but the order does not matter, thanks to python's keyword arguments).

Note: the above assumes you are using a fairly recent version of pubsub, with pubsub's default messaging protocol, rather than v1 or arg1. Try printing pub.VERSION_STR or pubsub.VERSION (the latter is in very latest only, wxpython phoenix, not likely the one you are using). Also, if there is a from wx.lib.pubsub import setupv1 or from wx.lib.pubsub import setuparg1 then you are using the old pubsub, which only accepts one message data, but arg name not needed (this could also explain your problem).

Otros consejos

Use named parameters.

wx.CallAfter(pub.sendMessage, 'Update', list1 = ListA, list2 = ListB)

This works:

import wx

from wx.lib.pubsub import setupkwargs
from wx.lib.pubsub import pub


class Controller(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Test")

        pub.subscribe(self.OnAppEvent, "APP_EVENT")
        wx.CallAfter(pub.sendMessage, "APP_EVENT", list1=('1','a'), list2=('2','b'))
        self.Show()

    def OnAppEvent(self, list1, list2):
        print list1, list2



app = wx.App()
controller = Controller()

app.MainLoop()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top