《wxPython 实战》book 程序可以直接运行,但不能从记事本 ++ 运行 [英] "wxPython in Action" book program works direct but not from notepad++

查看:34
本文介绍了《wxPython 实战》book 程序可以直接运行,但不能从记事本 ++ 运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注wxPython in Action"一书

Im following the book "wxPython in Action"

它给出了下面的例子

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

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!

  • 刚刚尝试了空闲" - 它有效!

请加分!

干杯

#!/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

推荐答案

Cheers PSS 我需要的线索

Cheers PSS the clue I needed

如果我改变线路

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

然后就可以了!

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

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! :(

我不得不使用os"来克服

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() 

这篇关于《wxPython 实战》book 程序可以直接运行,但不能从记事本 ++ 运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆