我想要一个具有一些切换功能的BitMapButton类: 首先按ON,OFF下一次按等。 这应该在Button的颜色中可见。 我尝试了以下内容(需要一些小的'off.gif','on.gif'文件):
import wx
class BitmapToggleButton(wx.BitmapButton):
"""make a Button, inherits wx.BitmapButton, add a toggleState"""
#----------------------------------------------------------------------
def __init__(self, parent, filename1, filename2):
"""Constructor"""
self.state = False
self.image1 = wx.Image(filename1, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.image2 = wx.Image(filename2, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#wx.BitmapButton.__init__(self, id=-1, bitmap=self.image2, pos=(10, 20), size = (300, 400))
self.image1 = wx.Image(filename1, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.image2 = wx.Image(filename2, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.button = wx.BitmapButton(parent, id=-1, bitmap=self.image1, pos=(10, 20), size = (self.image1.GetWidth()+5, self.image1.GetHeight()+5))
def OnClick(self, event):
"""does the toggling"""
if self.state:
self.state = False
self.button.SetBitmapLabel(self.image2)
else:
self.state = True
self.button.SetBitmapLabel(self.image1)
self.Refresh()
class MyFrame(wx.Frame):
"""make a frame, inherits wx.Frame, add a panel and button"""
def __init__(self):
# create a frame, no parent, default to wxID_ANY
wx.Frame.__init__(self, None, wx.ID_ANY, 'wxBitmapButton', pos=(300, 150), size=(300, 350))
#panel to display button
self.panel1 = wx.Panel(self, -1)
#test it
self.button = BitmapToggleButton(self, 'off.gif', 'on.gif')
self.Bind(wx.EVT_BUTTON, self.button.OnClick, self.button)
# show the frame
self.Show(True)
application = wx.PySimpleApp()
# call class MyFrame
f = MyFrame()
# start the event loop
application.MainLoop()
我承认有一些丢失的轨道,我必须从BitmapButton类继承,对,有什么问题?
JOH
答案 0 :(得分:0)
你不是。只需使用GenBitmapToggleButton
,请参阅wxPython演示示例(在Custom Controls / GenericButtons中)。
编辑:自至少2.8.9.1开始,请参阅Link。如果不确定如何使用,请仔细查看wxPython演示。
答案 1 :(得分:0)
我有wxPython 2.8。没有GenBitmapToggleButton 还是想念我?