使用wx.StaticBitmap.SetBitmap时出错

时间:2013-05-06 07:28:10

标签: bitmap wxpython

我在python中编写了一个可以从网上下载图片的程序。 对于每个下载的图片,它都在GUI中预览该图片,但每次都这样做我收到以下错误:

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/wx-2.8-gtk2-unicode/wx/_core.py", line 14665, in <lambda>
    lambda event: event.callable(*event.args, **event.kw) )
TypeError: 'NoneType' object is not callable
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/wx-2.8-gtk2-unicode/wx/_core.py", line 14665, in <lambda>
    lambda event: event.callable(*event.args, **event.kw) )
TypeError: 'NoneType' object is not callable
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/wx-2.8-gtk2-unicode/wx/_core.py", line 14665, in <lambda>
    lambda event: event.callable(*event.args, **event.kw) )
TypeError: 'NoneType' object is not callable
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/wx-2.8-gtk2-unicode/wx/_core.py", line 14665, in <lambda>
    lambda event: event.callable(*event.args, **event.kw) )
TypeError: 'NoneType' object is not callable

有趣的是:它仍然有效。但这个错误意味着什么?我该如何解决?

导致此错误的代码应为以下行:

wx.CallAfter(self.image.SetBitmap, wx.BitmapFromImage(wx.Image(imagePath, wx.BITMAP_TYPE_ANY).Rescale(width, height)))

1 个答案:

答案 0 :(得分:0)

我无法使用以下代码重现您的问题:

#!/usr/bin/env python

import wx

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Title")

        panel = wx.Panel(self)
        box = wx.BoxSizer(wx.VERTICAL)

        button = wx.Button(panel, wx.ID_ANY, "Load image")
        button.Bind(wx.EVT_BUTTON, self.ChangeImage)
        box.Add(button, 0, wx.ALL, 10)

        self.image = wx.StaticBitmap(panel)
        box.Add(self.image, 0, wx.ALL, 10)

        panel.SetSizer(box)
        panel.Layout()

    def ChangeImage(self, event):
        wx.CallAfter(self.image.SetBitmap, wx.BitmapFromImage(wx.Image("modernart.png", wx.BITMAP_TYPE_ANY).Rescale(150, 150)))

if __name__ == "__main__":
    app = wx.App()
    top = Frame()
    top.Show()
    app.MainLoop()

(您需要在同一文件夹中显示modernart.png图片。我会留待您创建。)

如果我使用self.image.setBitmapwx.CallAfter替换为None,那么我就可以重现您的错误。

您是否在上面显示的行以外的地方呼叫wx.CallAfter