gpt4 book ai didi

python - "wxPython in Action"图书程序可以直接运行,但不能从 Notepad++ 运行

转载 作者:行者123 更新时间:2023-12-01 05:13:24 24 4
gpt4 key购买 nike

我正在关注《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

最佳答案

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

关于python - "wxPython in Action"图书程序可以直接运行,但不能从 Notepad++ 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com