I am using wxPython on Python 2.7. I would like some help with creating a button with bitmap images.
我在Python 2.7上使用wxPython。我想帮助创建一个带位图图像的按钮。
I am using this video https://www.youtube.com/watch?v=Y7f0a7xbWHI, and I followed the codes and typed
我正在使用此视频https://www.youtube.com/watch?v=Y7f0a7xbWHI,我按照代码输入
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Frame aka window',size=(300,200))
panel=wx.Panel(self)
pic=wx.Image("back.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
self.button=wx.BitmapButton(panel, -1, pic, pos=(10,10))
self.Bind(wx.EVT_BUTTON, self.doMe, self.button)
self.button.SetDefault()
def doMe(self, event):
self.Destroy
to create a button with an image. I got an error stating Invalid Image. I saved the bitmap image in a folder that has .py file I am working with. I feel like I am saving the image in the wrong place? Thank you in advance.
创建带图像的按钮。我收到一条错误,指出无效图片。我将位图图像保存在我正在使用的.py文件的文件夹中。我觉得我把图像保存在错误的地方?先感谢您。
The error I received
我收到的错误
Traceback (most recent call last):
File "C:\Python27\FrameWindow.py", line 81, in <module>
frame=bucky(parent=None,id=-1)
File "C:\Python27\FrameWindow.py", line 17, in __init__
pic=wx.Image("back.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
File "C:\Python27\lib\site-packages\wx\core.py", line 708, in _Image_ConvertToBitmap
bmp = wx.Bitmap(self, depth)
wxAssertionError: C++ assertion "image.IsOk()" failed at ..\..\src \msw\bitmap.cpp(922) in wxBitmap::CreateFromImage(): invalid image
2 个解决方案
#1
0
Use wx.BITMAP_TYPE_ANY
rather than wx.BITMAP_TYPE_BMP
it takes the guessing out of whether it really is a BMP or not
or use wx.Bitmap()
directly.
使用wx.BITMAP_TYPE_ANY而不是wx.BITMAP_TYPE_BMP它会猜测它是否真的是BMP,或直接使用wx.Bitmap()。
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'Frame aka window',size=(300,200))
panel=wx.Panel(self)
#
# Use wx.BITMAP_TYPE_ANY rather than wx.BITMAP_TYPE_BMP
#
#pic=wx.Image("Discord.bmp", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#
# or use wx.Bitmap()
pic = wx.Bitmap("Discord.bmp", wx.BITMAP_TYPE_ANY)
#
self.button=wx.BitmapButton(panel, -1, pic, pos=(10,10))
self.Bind(wx.EVT_BUTTON, self.doMe, self.button)
self.Show()
def doMe(self, event):
self.Destroy()
if __name__ == "__main__":
app = wx.App()
frame = MainFrame()
app.MainLoop()
In principle you should name your image/s with a full pathname. If you have many images, then have an image directory and join
that directory name to your image names as you use them, which again gives you a full path (/home/images/back_button.bmp)
原则上,您应该使用完整路径名命名您的图像。如果你有很多图像,那么有一个图像目录,并在你使用它们时将该目录名加入你的图像名称,这又给你一个完整的路径(/home/images/back_button.bmp)
#2
0
I got it to work by adding
我通过添加来实现它
"locale = wx.Locale(wx.LANGUAGE_ENGLISH)"
under
class MainFrame(wx.Frame):
def __init__(self):
now I do not get the error message, and it runs as it should. The error code I received for this problem was:
现在我没有收到错误消息,它按预期运行。我收到的这个问题的错误代码是:
Traceback (most recent call last):
File "C:\Python27\panel test.py", line 21, in <module>
frame = MainFrame()
File "C:\Python27\panel test.py", line 11, in __init__
pic = wx.Bitmap("back.bmp", wx.BITMAP_TYPE_ANY)
wxAssertionError: C++ assertion "strcmp(setlocale(LC_ALL, NULL), "C") == 0" failed at ..\..\src\common\intl.cpp(1579) in wxLocale::GetInfo(): You probably called setlocale() directly instead of using wxLocale and now there is a mismatch between C/C++ and Windows locale.
Things are going to break, please only change locale by creating wxLocale objects to avoid this!
The error given by my primary question was solved by the codes given by Rolf by Saxony.
我的主要问题给出的错误是由Rolf by Saxony给出的代码解决的。
#1
0
Use wx.BITMAP_TYPE_ANY
rather than wx.BITMAP_TYPE_BMP
it takes the guessing out of whether it really is a BMP or not
or use wx.Bitmap()
directly.
使用wx.BITMAP_TYPE_ANY而不是wx.BITMAP_TYPE_BMP它会猜测它是否真的是BMP,或直接使用wx.Bitmap()。
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'Frame aka window',size=(300,200))
panel=wx.Panel(self)
#
# Use wx.BITMAP_TYPE_ANY rather than wx.BITMAP_TYPE_BMP
#
#pic=wx.Image("Discord.bmp", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#
# or use wx.Bitmap()
pic = wx.Bitmap("Discord.bmp", wx.BITMAP_TYPE_ANY)
#
self.button=wx.BitmapButton(panel, -1, pic, pos=(10,10))
self.Bind(wx.EVT_BUTTON, self.doMe, self.button)
self.Show()
def doMe(self, event):
self.Destroy()
if __name__ == "__main__":
app = wx.App()
frame = MainFrame()
app.MainLoop()
In principle you should name your image/s with a full pathname. If you have many images, then have an image directory and join
that directory name to your image names as you use them, which again gives you a full path (/home/images/back_button.bmp)
原则上,您应该使用完整路径名命名您的图像。如果你有很多图像,那么有一个图像目录,并在你使用它们时将该目录名加入你的图像名称,这又给你一个完整的路径(/home/images/back_button.bmp)
#2
0
I got it to work by adding
我通过添加来实现它
"locale = wx.Locale(wx.LANGUAGE_ENGLISH)"
under
class MainFrame(wx.Frame):
def __init__(self):
now I do not get the error message, and it runs as it should. The error code I received for this problem was:
现在我没有收到错误消息,它按预期运行。我收到的这个问题的错误代码是:
Traceback (most recent call last):
File "C:\Python27\panel test.py", line 21, in <module>
frame = MainFrame()
File "C:\Python27\panel test.py", line 11, in __init__
pic = wx.Bitmap("back.bmp", wx.BITMAP_TYPE_ANY)
wxAssertionError: C++ assertion "strcmp(setlocale(LC_ALL, NULL), "C") == 0" failed at ..\..\src\common\intl.cpp(1579) in wxLocale::GetInfo(): You probably called setlocale() directly instead of using wxLocale and now there is a mismatch between C/C++ and Windows locale.
Things are going to break, please only change locale by creating wxLocale objects to avoid this!
The error given by my primary question was solved by the codes given by Rolf by Saxony.
我的主要问题给出的错误是由Rolf by Saxony给出的代码解决的。