wxPython in Action"书的程序在直接运行时有效,但从Notepad++运行无效
我正在看一本书,叫做《wxPython in Action》
书里有下面这个例子
当我从我的“notepad++”运行这个程序时,出现了一堆错误(见下面),但是当我直接双击程序时,它就能正常工作!
- 我刚试了“Idle” - 也能正常工作!
有没有什么建议呢!
谢谢!
#!/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 个回答
2
谢谢你,PSS,这给了我需要的线索。
如果我修改这一行
image = wx.Image('//Server/users/xxxx/xxxx/xxxx/wxPython.jpg', wx.BITMAP_TYPE_JPEG)
那么它就能正常工作了!
我猜这可能和我的Windows桌面访问我的Ubuntu/Linux服务器有关!
我之前在处理那些烦人的反斜杠和正斜杠时遇到过麻烦! :(
我不得不使用"os"来解决这个问题
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()