2017-08-26 120 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

這是一個選擇淘汰對我自己的薰陶,因爲你原來的代碼工作得很好我的系統上。我個人認爲這更優雅,但這是您的選擇。 –