问题
Im following the book "wxPython in Action"
and it gives the example below
when I run the program from my "notepad++" I get a pile of errors (see bottom) however when I run the program direct by double clicking it works!
- just tried "Idle" - it works!
Any points please!
Cheers
#!/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:
Cheers PSS the clue I needed
If I change the line
image = wx.Image('//Server/users/xxxx/xxxx/xxxx/wxPython.jpg', wx.BITMAP_TYPE_JPEG)
then it works!
I ASSuME its something to do with my windows desktop accessing my Ubuntu/Linux Server!
I've had trouble with those pesky backslashes and forward slashes before! :(
I've had to use "os" to overcome
Basepathfile = os.path.dirname(os.path.abspath(__file__))
FileName = 'wxPython.jpg'
PrelimPathFile = os.path.join(Basepathfile, FileName )
PathFile = os.path.normpath(PrelimPathFile)
hence new program - BUT IT DOESN'T ANSWER WHY!
#!/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()
来源:https://stackoverflow.com/questions/23710761/wxpython-in-action-book-program-works-direct-but-not-from-notepad