0

我用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)))
4

1 回答 1

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.setBitmap在调用中替换为wx.CallAfterwith None,那么我能够重现您的错误。

wx.CallAfter除了上面显示的线路之外,您还打电话给其他地方吗?

于 2013-05-06T09:28:38.257 回答