【问题标题】:Bitmap button won´t be displayed位图按钮不会显示
【发布时间】:2014-05-05 19:18:42
【问题描述】:

这可能是个奇怪的问题,但我还没有找到类似的东西,所以我在问。我想显示我的位图按钮,我有代码,它工作得很好,但它不会显示按钮!如果我编写代码清空 IDLE,它正在工作并显示位图按钮,但在 IDLE 中,我已经导入了一些图片,它没有显示按钮!当然,按钮具有与其他图像不同的坐标。这是代码:

    import wx

类 GameFrame(wx.Frame):

def __init__(self,parent,id):

    wx.Frame.__init__(self, parent, id, "Project", style= wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX, size=(860, 640))
    wx.Frame.CenterOnScreen(self)

    panel = wx.Panel(self)
    self.SetBackgroundColour("green")

这里我导入一些图片,然后导入位图按钮:

            smallsoundstudioimg = wx.Image("Images/SmallSoundStudio.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
    self.sssbtn=wx.BitmapButton(panel, -1, smallsoundstudioimg, pos=(64,64))
    self.Bind(wx.EVT_BUTTON, self.sssaction, self.sssbtn)
    self.sssbtn.SetDefault()

def sssaction(self, event):
    print "Small Sound Studio"

    if __name__=='__main__':
    app=wx.App()
    frame=GameFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

请帮助别人!
非常感谢大家的建议!

【问题讨论】:

    标签: button bitmap wxpython


    【解决方案1】:

    您的代码对我有用。我稍微编辑它以使用 wx.ArtProvider 因为你没有提供你正在使用的图像。这是我的版本:

    import wx
    
    class GameFrame(wx.Frame):
        def __init__(self,parent,id):
    
            wx.Frame.__init__(self, parent, id, "Project", style= wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX, size=(860, 640))
            wx.Frame.CenterOnScreen(self)
    
            panel = wx.Panel(self)
            self.SetBackgroundColour("green")
    
            smallsoundstudioimg = wx.ArtProvider.GetBitmap(wx.ART_NEW, wx.ART_TOOLBAR, (36,36))
    
            self.sssbtn=wx.BitmapButton(panel, -1, smallsoundstudioimg, pos=(64,64))
            self.Bind(wx.EVT_BUTTON, self.sssaction, self.sssbtn)
            self.sssbtn.SetDefault()
    
        def sssaction(self, event):
            print "Small Sound Studio"
    
    if __name__=='__main__':
        app=wx.App()
        frame=GameFrame(parent=None,id=-1)
        frame.Show()
        app.MainLoop()
    

    【讨论】:

      【解决方案2】:

      我发现问题出在哪里。我已经向其他 .py 文档编写了代码,它在框架的右上角只显示了一个小方块。如果坐标为 0, 0 它显示按钮的一小部分,当我单击它时,它会打印 Clicked。这是代码:

      import wx
      
      class MyFrame(wx.Frame):
      def __init__(self):
          wx.Frame.__init__(self, None, wx.ID_ANY, 'Bitmap Button', pos=(300, 150), size=(300, 350))
          self.panel1 = wx.Panel(self, -1)
      
          imageFile = "SmallSoundStudio.png"
          image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
          self.button1 = wx.BitmapButton(self.panel1, id=-1, bitmap=image1, pos=(10, 10))
          self.button1.Bind(wx.EVT_BUTTON, self.button1Click)
      
          roadimg = wx.Bitmap("SmallSoundStudio.png")
          cntrlrdimg = wx.StaticBitmap(self, -1, roadimg)
          cntrlrdimg.SetPosition((250, 250))
      
          self.Show(True)
      
      def button1Click(self,event):
          print "Clicked"
      
      application = wx.App()
      window = MyFrame()
      application.MainLoop()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-20
        • 2015-07-06
        • 2014-12-06
        相关资源
        最近更新 更多