還是新的使用wx.python,所以請讓我知道如果我做錯什麼。我正在嘗試創建一個僞位圖切換按鈕。我有2樓以上的位圖的按鈕與藍色的初始背景,並在單擊時,一個將其背景應該變爲綠色。發生這種情況時,所有其他按鈕應該變回藍色,但它們保持綠色。有任何想法嗎?wx.python GenBitmapButton SetBackgroundColour僅改變第一次
我在下面重新創建我的問題。
*編輯: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()
工作正常在Linux上。你嘗試過使用'wx.BLUE'和'wx.GREEN'而不是「藍色」和「綠色」嗎?這雖然有點像冰雹瑪麗。 –
我忘了提及我在Windows上。我已經嘗試過,沒有工作。 – MikeJewski
嘗試刪除'大小=(bmp.GetWidth()+ 10,bmp.GetHeight()+ 10)'上的按鈕中的一個。 –