【问题标题】:Assertion Error WxPython断言错误 WxPython
【发布时间】:2018-02-21 03:07:58
【问题描述】:

我有这个代码:

import wx

class application(wx.Frame):

    def __init__(self, *args, **kw):
        super(application, self).__init__(*args, **kw)
        panel = wx.Panel(self)
        # Header
        Header = wx.StaticText(panel, label="Browse", pos=(25, 25))
        HeaderFont = Header.GetFont()
        HeaderFont.PointSize += 10
        HeaderFont = HeaderFont.Bold()
        Header.SetFont(HeaderFont)
        # displaying everything in 'override_contents.py'
        from override_contents import contents
        posx = 25
        posy = 60
        change = 0
        def contentButtonDisplay(panel=panel, id=wx.ID_ANY, label="None", pos=(0,0), event=None, i=None):
            wx.Button(panel, id, label, pos).Bind(wx.EVT_BUTTON, event)
        for i in contents:
            contentButtonDisplay(label=i["name"], pos=(posx, posy), event=i["event"])
            if change == 5:
                posy += 50
                posx = 0
                change = 0
            else:
                posx += len(i) * 10
                change += 1

app = wx.App()
frm = application(None, title='Browse')
frm.Show()
app.MainLoop()

还有override_contents

contents=[
    {
        "name" : "PyGame",
        "event": "print('Hello!')"
    }
]

当我运行它时,我得到了

Traceback (most recent call last):
  File "C:/Users/???'s Coding/PycharmProjects/Interface/browse.py", line 32, in <module>
    frm = application(None, title='Browse')
  File "C:/Users/???'s Coding/PycharmProjects/Interface/browse.py", line 22, in __init__
    contentButtonDisplay(label=i["name"], pos=(posx, posy), event=i["event"])
  File "C:/Users/???'s Coding/PycharmProjects/Interface/browse.py", line 20, in contentButtonDisplay
    i.Bind(wx.EVT_BUTTON, event)
  File "C:\Users\???'s Coding\Desktop\Python Intall Directory\lib\site-packages\wx\core.py", line 1350, in _EvtHandler_Bind
    assert callable(handler) or handler is None
AssertionError

正如代码中所述,它将成为一个媒体共享平台。
当我打电话给.Bind 时,似乎发生了一些可疑的事情。
有人可以帮我澄清一下这里发生了什么吗?
谢谢。

【问题讨论】:

  • from override_contents import contents ???????
  • 我会添加到

标签: python-3.x event-handling wxpython


【解决方案1】:

原来我犯了一个菜鸟错误
override_contents 中,我传递了一个字符串而不是代码。
我所要做的就是:

import wx
def onPygame():
    wx.MessageBox("Pygame!", "Pygame!")
# ...
"event": lambda _: onPygame
# ...

所以,是的。菜鸟失误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2018-05-19
    • 2014-09-27
    • 2014-03-26
    • 1970-01-01
    相关资源
    最近更新 更多