使用位图图像创建按钮wxPython

2024-04-25 23:55:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python2.7上使用wxPython。 我想要一些与位图图像创建按钮的帮助。在

我在用这个视频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

创建带有图像的按钮。我在声明无效图像时出错。 我将位图图像保存在一个文件夹中,其中有我正在使用的.py文件。 我觉得我把图像保存在错误的地方了? 提前谢谢你。在

我收到的错误

^{pr2}$

Tags: 图像selfidinitdef错误wxpythonbutton
2条回答

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

enter image description here

原则上,您应该用完整的路径名命名映像。如果您有许多映像,那么就有一个映像目录,并在您使用它们时将该目录名join添加到您的映像名中,这将再次为您提供完整的路径(/home/images/back_按钮.bmp)在

我通过添加

"locale = wx.Locale(wx.LANGUAGE_ENGLISH)"

低于

^{pr2}$

现在我没有收到错误消息,它正常运行。 我收到的这个问题的错误代码是:

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!

我的主要问题给出的错误由萨克森州罗尔夫给出的代码解决了。在

相关问题 更多 >