2017-08-26 124 views
0

还是新的使用wx.python,所以请让我知道如果我做错什么。我正在尝试创建一个伪位图切换按钮。我有2楼以上的位图的按钮与蓝色的初始背景,并在单击时,一个将其背景应该变为绿色。发生这种情况时,所有其他按钮应该变回蓝色,但它们保持绿色。有任何想法吗?wx.python GenBitmapButton SetBackgroundColour仅改变第一次

我在下面重新创建我的问题。

BMP图像使用,但图像没有关系:enter image description here

*编辑:GenBitmapToggleButton突然决定现在的工作,所以我会使用。我将要离开这个,因为它仍然是一个奇怪的错误,因为它似乎在Linux上工作,而不是在Windows上工作。

import wx 
import wx.lib.buttons as buttons 

class MainFrame(wx.Frame): 

#---------------------------------------------------------------------- 
    def __init__(self): 

     wx.Frame.__init__(self, None, title="Test",size=(800,800)) 
     panel = wx.Panel(self,-1,name="panel") 

     bmp = wx.Bitmap("Discord.bmp", wx.BITMAP_TYPE_ANY) 

     self.Button1 = buttons.GenBitmapButton(panel,bitmap=bmp,pos=(200,400),size=(bmp.GetWidth()+10, bmp.GetHeight()+10),style=wx.NO_BORDER,name="Button1") 
     self.Button1.SetBackgroundColour("Blue") 

     self.Button2 = buttons.GenBitmapButton(panel,bitmap=bmp,pos=(600,400),size=(bmp.GetWidth()+10, bmp.GetHeight()+10),style=wx.NO_BORDER,name="Button2") 
     self.Button2.SetBackgroundColour("Blue") 
     self.Bind(wx.EVT_BUTTON, self.OnClick) 

     self.BitmapButtons = [self.Button1,self.Button2] 
     self.Show() 

    def OnClick(self,event): 
     parent = event.GetEventObject().GetParent().GetName() 
     name = event.GetEventObject().GetName() 

     if parent == "panel": 
      for i in range(0,len(self.BitmapButtons)): 
       buttonName = self.BitmapButtons[i].GetName() 
       if buttonName == name: 
        self.BitmapButtons[i].SetBackgroundColour("Green") 
       else: 
        self.BitmapButtons[i].SetBackgroundColour("Blue") 


#---------------------------------------------------------------------- 
if __name__ == "__main__": 
     app = wx.App(False) 
     frame = MainFrame() 
     app.MainLoop() 
+0

工作正常在Linux上。你尝试过使用'wx.BLUE'和'wx.GREEN'而不是“蓝色”和“绿色”吗?这虽然有点像冰雹玛丽。 –

+0

我忘了提及我在Windows上。我已经尝试过,没有工作。 – MikeJewski

+0

尝试删除'大小=(bmp.GetWidth()+ 10,bmp.GetHeight()+ 10)'上的按钮中的一个。 –

回答

1

这是一种选择,它采用了按键和多个图像的state达到你在做什么,我要说的应该是这样做的首选方法。
在这里,我只使用2幅图像,但你可以使用4个,每个state
正常状态
聚焦状态
选择状态
和Disable状态

import wx 
import wx.lib.buttons as buttons 

class MainFrame(wx.Frame): 

#---------------------------------------------------------------------- 
    def __init__(self): 

     wx.Frame.__init__(self, None, title="Test",size=(800,800)) 
     panel = wx.Panel(self,-1,name="panel") 

     bmp = wx.Bitmap("Discord.png", wx.BITMAP_TYPE_ANY) 
     bmp2 = wx.Bitmap("Discord1.png", wx.BITMAP_TYPE_ANY) 

     self.Button1 = buttons.GenBitmapButton(panel,bitmap=bmp,pos=(100,100),name="Button1") 
     self.Button2 = buttons.GenBitmapButton(panel,bitmap=bmp,pos=(200,100),name="Button2") 
     self.Button3 = buttons.GenBitmapButton(panel,bitmap=bmp,pos=(300,100),name="Button3") 
     self.BitmapButtons = [self.Button1,self.Button2,self.Button3] 
     for i in range(0,len(self.BitmapButtons)): 
      self.BitmapButtons[i].SetBitmapLabel(bmp) 
      self.BitmapButtons[i].SetBitmapFocus(bmp2) 
      self.BitmapButtons[i].SetBitmapSelected(bmp2) 
     self.Show() 
#---------------------------------------------------------------------- 
if __name__ == "__main__": 
     app = wx.App(False) 
     frame = MainFrame() 
     app.MainLoop() 

enter image description here

+0

的计划是使用相同的图像,只是改变每个人的背景。这是由于切换版本不符合UI的外观。 – MikeJewski

+0

这是一个选择淘汰对我自己的熏陶,因为你原来的代码工作得很好我的系统上。我个人认为这更优雅,但这是您的选择。 –