【问题标题】:wx.python GenBitmapButton SetBackgroundColour only changes the first timewx.python GenBitmapButton SetBackgroundColour 只在第一次改变
【发布时间】:2017-08-26 03:28:24
【问题描述】:

还是新手使用 wx.python,所以如果我做错了什么请告诉我。我正在尝试创建一个伪位图切换按钮。我有 2 个或多个初始背景为蓝色的位图按钮,当单击一个时,它的背景应该变为绿色。发生这种情况时,所有其他按钮都应该变回蓝色,但它们保持绿色。有任何想法吗?

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

使用BMP图片,但图片无所谓:

*编辑: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.BLUEwx.GREEN 而不是“蓝色”和“绿色”?不过,这有点像冰雹玛丽。
  • 我忘了说我在 Windows 上。我已经试过了,还是不行。
  • 尝试删除其中一个按钮上的size=(bmp.GetWidth()+10, bmp.GetHeight()+10)
  • 刚试了一下,没啥变化

标签: python button wxpython


【解决方案1】:

这是一个选项,它使用按钮的state 和多个图像来实现您正在做的事情,我认为应该是首选的方法。
在这里,我只使用 2 张图片,但您可以使用 4 张,每张一张 state
正常状态
专注状态
选定状态
和禁用状态

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()

【讨论】:

  • 计划是使用相同的图像,只是改变每个图像的背景。这是由于切换版本与 UI 的外观不匹配。
  • 这是一个为我自己的启迪而淘汰的选项,因为您的原始代码在我的系统上运行良好。我个人认为这更优雅,但这是你的选择。
猜你喜欢
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
  • 1970-01-01
  • 2018-09-09
相关资源
最近更新 更多