" wxPython in Action"书籍程序直接但不是来自记事本++

时间:2014-05-17 11:26:39

标签: python wxpython

我跟随着这本书" wxPython in Action"

它给出了下面的例子

当我从我的" notepad ++"运行程序时我得到了一堆错误(见底部)但是当我通过双击直接运行程序时它可以工作!

  • 刚试过"空闲" - 它有效!

请点!

干杯

#!/usr/bin/env python 
"""Hello, wxPython! program.""" 
import wx 
class Frame(wx.Frame): 
    """Frame class that displays an image.""" 
    def __init__(self, image, parent=None, id=-1, 
                pos=wx.DefaultPosition, 
                title='Hello, wxPython!'): 
        """Create a Frame instance and display image.""" 
        temp = image.ConvertToBitmap() 
        size = temp.GetWidth(), temp.GetHeight() 
        wx.Frame.__init__(self, parent, id, title, pos, size) 
        self.bmp = wx.StaticBitmap(parent=self, bitmap=temp) 
class App(wx.App): 
    """Application class.""" 
    def OnInit(self): 
        image = wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG) 
        self.frame = Frame(image) 
        self.frame.Show() 
        self.SetTopWindow(self.frame) 
        return True 
def main(): 
    app = App() 
    app.MainLoop() 
if __name__ == '__main__': 
    main() 
Traceback (most recent call last):
  File "Z:\Programming\Python2.7\temp.py", line 26, in <module>
    main()
  File "Z:\Programming\Python 2.7\temp.py", line 23, in main
    app = App()
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 8631, in __init__
    self._BootstrapApp()
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 8196, in _BootstrapApp
    return _core_.PyApp__BootstrapApp(*args, **kwargs)
  File "Z:\Programming\Python 2.7\temp.py", line 18, in OnInit
    self.frame = Frame(image)
  File "Z:\Programming\Python 2.7\temp.py", line 10, in __init__
    temp = image.ConvertToBitmap()
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 3646, in ConvertToBitmap
    return _core_.Image_ConvertToBitmap(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "image.IsOk()" failed at ..\..\src\msw\bitmap.cpp(820) in wxBitmap::CreateFromImage(): invalid image

1 个答案:

答案 0 :(得分:2)

为PSS提供我需要的线索

如果我改变了行

image = wx.Image('//Server/users/xxxx/xxxx/xxxx/wxPython.jpg', wx.BITMAP_TYPE_JPEG)

然后它有效!

我ASSuME与我的Windows桌面访问我的Ubuntu / Linux服务器有关!

我之前遇到过那些讨厌的反斜杠和正斜杠的问题! :(

我必须使用&#34; os&#34;克服

Basepathfile = os.path.dirname(os.path.abspath(__file__))
FileName = 'wxPython.jpg'
PrelimPathFile = os.path.join(Basepathfile, FileName )
PathFile = os.path.normpath(PrelimPathFile)

因此新计划 - 但它有什么需要回答的问题!

#!/usr/bin/env python 
"""Hello, wxPython! program.""" 
import wx 
import os

class Frame(wx.Frame): 
    """Frame class that displays an image.""" 
    def __init__(self, image, parent=None, id=-1, 
                pos=wx.DefaultPosition, 
                title='Hello, wxPython!'): 
        """Create a Frame instance and display image.""" 
        temp = image.ConvertToBitmap() 
        size = temp.GetWidth(), temp.GetHeight() 
        wx.Frame.__init__(self, parent, id, title, pos, size) 
        self.bmp = wx.StaticBitmap(parent=self, bitmap=temp) 
class App(wx.App): 
    """Application class.""" 
    def OnInit(self): 
        Basepathfile = os.path.dirname(os.path.abspath(__file__))
        FileName = 'wxPython.jpg'
        PrelimPathFile = os.path.join(Basepathfile, FileName )
        PathFile = os.path.normpath(PrelimPathFile)
        image = wx.Image(PathFile, wx.BITMAP_TYPE_JPEG) 
        self.frame = Frame(image) 
        self.frame.Show() 
        self.SetTopWindow(self.frame) 
        return True 
def main(): 
    app = App() 
    app.MainLoop() 
if __name__ == '__main__': 
    main()