1

我正在尝试使用 PyPubSub 在两个 wxpython 窗口之间传递数据。下面我已经削减了基础的代码。当我在主窗口的 textcntrl 中填写数据并点击按钮将其传递到第二个窗口(CoordFrame)时,我得到了以下错误 -

TypeError:sendMessage() 接受 2 个位置参数,但给出了 3 个

我能找到的每个示例都显示在 pub.sendMessage 中传递了两个参数;这就是我认为我正在做的事情。传递的第三个参数是什么,它是从哪里来的,我该如何阻止它?我尝试了各种参数组合和包括二进制在内的数据类型。

'''

from pubsub import pub
import wx

class MainFrame (wx.Frame):
    def __init__(self, parent, title):
        super(MainFrame, self).__init__(parent, title = title, size = (200,200))
        self.panel = MainPanel(self)

class CoordFrame (wx.Frame):
    def __init__(self, parent, title):
        super(CoordFrame, self).__init__(parent, title = title, size = (200,200))
        self.panel = CoordPanel(self)


class MainPanel(wx.Panel):
    def __init__(self, parent):
        super(MainPanel, self).__init__(parent)
        vsizer = wx.BoxSizer(wx.VERTICAL)
    
        self.tbxNewMap = wx.TextCtrl(self, id=1001, pos=(20,20), size = (50,20) ,style = wx.TE_CENTER|wx.TE_NOHIDESEL|wx.TE_PROCESS_ENTER)
        vsizer.Add(self.tbxNewMap)

        self.btnEnterNewMap = wx.Button(self, id=1002, label = "New Data", pos = (20,80), size = (80,40))
        vsizer.Add(self.btnEnterNewMap,0,wx.EXPAND)
        self.Bind(wx.EVT_BUTTON, self.onButtonNewMap, id=1002)

    def onButtonNewMap(self,event):
        temp = self.tbxNewMap.GetValue()
        pub.sendMessage("coord_listener", temp)
        coordframe = CoordFrame(None,"Entry")
        coordframe.Show()

class CoordPanel(wx.Panel):
    def __init__(self, parent):
        super(CoordPanel, self).__init__(parent)
        vsizer = wx.BoxSizer(wx.VERTICAL)
        pub.subscribe(self.coord_listener, "coord_listener")

        self.tbxNewMapNumber = wx.TextCtrl(self, id=1000, pos=(20,20), size = (50,20), style = wx.TE_CENTER|wx.TE_NOHIDESEL|wx.TE_PROCESS_ENTER)
        vsizer.Add(self.tbxNewMapNumber)
        
    def coord_listener(self, message):
        newmapnum = message
        self.tbxNewMapNumber.SetValue(newmapnum)
        self.tbxNewMapNumber.Refresh()


class GMDash(wx.App):
    def OnInit(self):
        self.mainframe = MainFrame(parent = None, title = "Dashboard")
        self.mainframe.Show()
        return True

app = GMDash()
app.MainLoop()

'''

4

1 回答 1

0

在调用中使用命名参数,sendmessage您将需要注册before发送消息的侦听器,而不是像您当前所做的那样。

见下文:

from pubsub import pub
import wx

class MainFrame (wx.Frame):
    def __init__(self, parent, title):
        super(MainFrame, self).__init__(parent, title = title, size = (200,200))
        self.panel = MainPanel(self)

class CoordFrame (wx.Frame):
    def __init__(self, parent, title):
        super(CoordFrame, self).__init__(parent, title = title, size = (200,200))
        self.panel = CoordPanel(self)


class MainPanel(wx.Panel):
    def __init__(self, parent):
        super(MainPanel, self).__init__(parent)
        vsizer = wx.BoxSizer(wx.VERTICAL)
    
        self.tbxNewMap = wx.TextCtrl(self, id=1001, pos=(20,20), size = (50,20) ,style = wx.TE_CENTER|wx.TE_NOHIDESEL|wx.TE_PROCESS_ENTER)
        vsizer.Add(self.tbxNewMap)

        self.btnEnterNewMap = wx.Button(self, id=1002, label = "New Data", pos = (20,80), size = (80,40))
        vsizer.Add(self.btnEnterNewMap,0,wx.EXPAND)
        self.Bind(wx.EVT_BUTTON, self.onButtonNewMap, id=1002)
    #Register the subscrption *before* sending the message
        self.coordframe = CoordFrame(None,"Entry")

    def onButtonNewMap(self,event):
        temp = self.tbxNewMap.GetValue()
        pub.sendMessage("coord_listener", message=temp)
        #coordframe = CoordFrame(None,"Entry")
        self.coordframe.Show()

class CoordPanel(wx.Panel):
    def __init__(self, parent):
        super(CoordPanel, self).__init__(parent)
        vsizer = wx.BoxSizer(wx.VERTICAL)
        pub.subscribe(self.coord_listener, "coord_listener")

        self.tbxNewMapNumber = wx.TextCtrl(self, id=1000, pos=(20,20), size = (50,20), style = wx.TE_CENTER|wx.TE_NOHIDESEL|wx.TE_PROCESS_ENTER)
        vsizer.Add(self.tbxNewMapNumber)
        
    def coord_listener(self, message):
        print(message)
        newmapnum = message
        self.tbxNewMapNumber.SetValue(newmapnum)
        self.tbxNewMapNumber.Refresh()


class GMDash(wx.App):
    def OnInit(self):
        self.mainframe = MainFrame(parent = None, title = "Dashboard")
        self.mainframe.Show()
        return True

app = GMDash()
app.MainLoop()

在此处输入图像描述

于 2021-05-27T09:16:39.383 回答