首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WxPython PyPubSub,使用咖喱函数不起作用

WxPython PyPubSub,使用咖喱函数不起作用
EN

Stack Overflow用户
提问于 2021-10-07 14:55:10
回答 1查看 97关注 0票数 0

我正在使用PyPubSub模块的WxPython来发送消息,我希望有一个函数订阅一个主题,其中函数有一些临时参数。不幸的是,它似乎并没有像我所期望的那样使用咖喱功能。

代码语言:javascript
复制
from functools import partial
import time

import wx
from pubsub import pub

def MakeStatusBar(top_frame):

    statusbar = top_frame.CreateStatusBar()
    statusbar.SetFieldsCount(2)
    statusbar.SetStatusWidths([400, 400])

    # pub.subscribe(listener=got_msg, topicName='game.new')  # Works, when alone

    status_func = partial(got_status_msg, statusbar)
    print(f"status_func[callable={callable(status_func)}] = {status_func}")
    status_func(some_data='Directly calling the curried-function')  # Calling the function directly - works
    pub.subscribe(listener=status_func, topicName='game.new')  # Breaks

    return statusbar

def got_msg(some_data):
    print(f"\n*** got_msg: data = {some_data}\n")

def got_status_msg(status_bar, some_data):
    print(f"\n*** got_status_msg, args = {some_data}\n")
    status_bar.SetStatusText(some_data, 0)

class topFrame(wx.Frame):
    def __init__(self, parent):
        super().__init__(parent, wx.ID_ANY, title='testing', size=(500, 500))
        self.status_bar = (MakeStatusBar(self))

class myApp(wx.App):
    def __init__(self):
        wx.App.__init__(self, redirect=True, filename='errlog.txt')  # To log error to a file
        # super().__init__(self)  # Doesn't take redirect? Weird.

        self.topFrame = topFrame(None)
        self.SetTopWindow(self.topFrame)
        self.topFrame.Show()
        print(f"in 2 seconds, will send a message to the Statusbar")
        time.sleep(2)
        print(f"sending...\n")

        try:
            pub.sendMessage(topicName='game.new', some_data='publishing a new-game message')
        except Exception as ex:
            print(f"WTF? got {str(ex)}\n")
        print(f"message has been sent.\n")

if __name__ == '__main__':
    my_app = myApp()
    my_app.MainLoop()

我得到的错误是:

代码语言:javascript
复制
Traceback (most recent call last):
  File "/Code/wxpy/hex_test/src/wx/menu_bar.py", line 11, in new_game_msg
    pub.sendMessage(topicName='game.new', some_data='this is a test message')
    raise SenderUnknownMsgDataError(self.topicNameTuple,
pubsub.core.topicargspec.SenderUnknownMsgDataError: 
Some optional args unknown in call to sendMessage('('game', 'new')', some_data): some_data

那么,我如何将一个监听器函数传递给至少有一个参数的主题呢?显然还有其他的方法(比如全局变量),但我觉得这应该是可以用部分函数实现的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-07 15:22:39

嗯,盯着它看,再读一遍文档,是的,有个解决办法。它已经被放入PyPubSub中了,侦听器本身已经允许使用草率的参数。我可以直接传入我想要的StatusBar参数。

代码语言:javascript
复制
    # status_func = partial(got_status_msg, statusbar)
    # print(f"status_func[callable={callable(status_func)}] = {status_func}")
    # status_func(some_data='Directly calling the curried-function')  # Calling the function directly - works

    pub.subscribe(listener=got_status_msg, topicName='game.new', status_bar=statusbar)  # Now works

因此,显然,PyPubSub的开发人员预料到了这一用途。干得好。:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69483301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档