【发布时间】:2017-08-26 03:28:24
【问题描述】:
还是新手使用 wx.python,所以如果我做错了什么请告诉我。我正在尝试创建一个伪位图切换按钮。我有 2 个或多个初始背景为蓝色的位图按钮,当单击一个时,它的背景应该变为绿色。发生这种情况时,所有其他按钮都应该变回蓝色,但它们保持绿色。有任何想法吗?
我在下面重新创建了我的问题。
*编辑: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 上。我已经试过了,还是不行。
-
尝试删除其中一个按钮上的
size=(bmp.GetWidth()+10, bmp.GetHeight()+10)。 -
刚试了一下,没啥变化