如何子类化wxpython StaticBitmap以添加属性

时间:2012-11-18 19:50:36

标签: wxpython subclass super

我正在尝试向wxpython静态位图添加“value”属性,但我无法想象如何实例化我创建的子类。当我尝试创建它时,我收到错误'期望一个2元组的整数或一个wxPoint对象。'

任何人都可以告诉我缺少什么吗?

由于

class StaticBmpButton(wx.StaticBitmap):
    def __init__(self, parent,id,bmp_off,bmp_on,bmp_over):
        super(wx.StaticBitmap,self).__init__(parent,id,bitmap)
        self.value=False

2 个答案:

答案 0 :(得分:0)

听起来你正在创建自己的自定义按钮?您可能希望将其创建为PyControl。我使用这种方法创建了一个fadebutton,可以将其作为自定义控件导入并直接替换为wx.Button。

class StaticBmpButton(wx.PyControl):
    def __init__(self, parent, id=wx.ID_ANY, label="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.NO_BORDER, validator=wx.DefaultValidator, name="StaticBmpButton"):
        wx.PyControl.__init__(self, parent=parent, id=id, pos=pos, size=size, style=wx.NO_BORDER, validator=validator, name=name)

以下是自定义PyContol按钮基本框架的链接: http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=17567 我现在无法加载python-forum。如果您需要我发布该页面的代码,请告诉我。

编辑:我猜python-forum不再存在了。这是缓存页面的链接: http://cc.bingj.com/cache.aspx?q=BlockButton.py+gradient&d=4580007883572708&mkt=en-US&setlang=en-US&w=7g-uOMgExcAivyB_VE3bzv2kstr-ww21

编辑以显示示例:

这只是一个如何在PyControl框架中使用位图的简单示例。可能有更好的方法来做到这一点,但它会告诉你这个想法。对不起,代码太长了,但我想嵌入图片,以便更容易演示。

#!/usr/bin/env python
import wx
from wx.lib.embeddedimage import PyEmbeddedImage

button_blue = PyEmbeddedImage(
    "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")
getbutton_blueData = button_blue.GetData
getbutton_blueImage = button_blue.GetImage
getbutton_blueBitmap = button_blue.GetBitmap

button_gray = PyEmbeddedImage(
    "iVBORw0KGgoAAAANSUhEUgAAAGQAAAAaCAIAAADOqG6yAAAAA3NCSVQICAjb4U/gAAADl0lE"
    "QVRYheWZOU4sMRCGXW23hkVCQoQkHAAJiSWflJgTwMm4AAlHIJsQAkICECFBo/ZML15eYJ7H"
    "4717hveEqKBll2vc9uff5TaAlBIhJKVkjFFKX19fZ7PZ09PT29tbVVWMMSEE+h1WFEVZlvv7"
    "+0dHR6enp2dnZ4eHhzs7O0VRAABCCOq6fnh4uL29fXl5AYDJZLK7u7u9vU0IAQAVtBHzdmU6"
    "dTn0UssfryY93lYppRCi7/u6rquqms/nAHB8fHxzc3N+fg7X19fPz8+MsbIsy7IEAMZY3/fr"
    "CCo+7a9VcmLiNAfFh6BE8JkFjDEhBGMshPj8/KSUbm1tTadT8v7+rrj0fd/3vW/uafuOCWfG"
    "e5vW7F8I0batKigmnPOPjw+CEFJpa6j9IEDxMG+8cjLGOOfaT9xxh2yEnkPBm413fzKCjtsk"
    "hOi6zkxHBP1NbO748t+ROaZ42KAFyIwf2r8uSykXi0XXdSYZj7J+Ip3R/XudnHNKadu2loZW"
    "YOm9mj+HDQL67v4z5da2bV3XZqrStoRlkvpVdPS+a5qGUurFpMxW1tB3rDOH0SdGZnzOeIQQ"
    "TdPM53PGGEqZraz/uMJu6zfRYYw1TdN1XUREXkvnrH+wBTbSub7kir/GOeecq+vtRm64Wadh"
    "XA7KhBBSSjVWPW5vZKSTpIX6dP3jvrTjtrIN9fUahXeEWjFNxApWVhSFWY3QyQE3DlCyPIIm"
    "MaEoWG7m4pyrq7UVbMZoi1cjzqR5p2c5k1xcp1WIQCSqDQAwxhhjhUDnQs0IAAj5kmHyeEry"
    "GgErSSpfSiE6oar2LLdhURSEEAXLlBLGOHQ25WTxjZDSlrn1cmB5q15S+tBcikUpCwDUmerF"
    "ZFYHfWetsxlzclY+nZxWk9TKRVqPWMFS92xzM4YyvbfqLaBN6CsnNyGHiOuJoPE+zaGunIbq"
    "jMMYI5+IQs9QFUV5DYIV2X2ZKSkflhlpK0sneISQzuXm/AeRGi20pIWUlZ+JMp+6AADquYRl"
    "vT4iq3ythRTnlq3RJBklMcVPtDgdq6rGtgJLVay2pL4iNHPwueW45ejIC0Kvh56g9UTGmplV"
    "5KwlAJDLy8v7+/uqqhaLhfrHmYUjpxAhGGeXRJakgwKisPwageuMkGKMqT8BHhwcTKdTEEJQ"
    "Smez2d3d3ePjo0KmPrjMM9E7K+88k2EjEpYJZU1PMvfpG7iUcm9v7+Li4urq6uTkZDKZ/AGs"
    "3amq94qX9AAAAABJRU5ErkJggg==")
getbutton_grayData = button_gray.GetData
getbutton_grayImage = button_gray.GetImage
getbutton_grayBitmap = button_gray.GetBitmap

#----------------------------------------------------------------------
button_green_bright = PyEmbeddedImage(
    "iVBORw0KGgoAAAANSUhEUgAAAGQAAAAaCAIAAADOqG6yAAAAA3NCSVQICAjb4U/gAAAIUUlE"
    "QVRYhY1YTastRxVdu7rvve/Fhxk8IgiKQgQDKookg0imOhGcOdI/IP4G/4cDZw6c6lhxGB2I"
    "hCBElBANCD5Q8/LyzOnT3VXLQe2v6nNu8FzuOd3V1VW111577V0lJAEAbK2t2/qfj/717j/e"
    "eeuvv//38yfnbVnOp33fewcCIAjaTfpQ/+1OEA2HrukjcSX3dvrkF//v59K/ZGyxeQUApml6"
    "cPvgdn744guPv/7ya1/+wtcef/qlu9uHRYqIAJDlfPrTe3/8xW9/+ta7b251nafbT909enj3"
    "6Ga+EQj7B/2r/0AbFDXDacCv3whAgfCKAWKrxwGpvizrLYffg5m5a0JC1DbxaUSfSx8/7iU/"
    "gogIya2uz08fPfvv02U9FZFXPv+NH377x6++8ob85Gc/+t07v1m20025vZ1eKCJ725f1tNet"
    "sSlACRYDCd4EiN4aiQZz3YhksCTrHB2zXgRXhomnAhm7yfF5Bt4BSlOJT+2N0sft/1OZb+e7"
    "ucwkP/z4g+fLs6lML3/2K/P7//zbeV1rZeX51M6ZSoHOgFf8woHrkytaoviN4CQMsr/zsxwU"
    "YX380ZtGNwRE8YK/NTpigC6gGiHbpa7r2gPrtJ72re6of37v7bmRrbHVpnHWNOCMUz3w+l1E"
    "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K8ODhoze++h1prX34/INf/+FXv3zz539/8pe97WWayixl"
    "BkpHCURTodD49PNU9/ax/LZTWrvNy0ng5pVeQJVeHNHJfSwIO2sSQv7WmEQyItlV4n8Ed7SK"
    "WmtB+dxLX/zet37w3de///jFz/wP8WUTUVqRAEEAAAAASUVORK5CYII=")
getbutton_green_brightData = button_green_bright.GetData
getbutton_green_brightImage = button_green_bright.GetImage
getbutton_green_brightBitmap = button_green_bright.GetBitmap

#----------------------------------------------------------------------
button_red = PyEmbeddedImage(
    "iVBORw0KGgoAAAANSUhEUgAAAGQAAAAaCAIAAADOqG6yAAAAA3NCSVQICAjb4U/gAAAEWElE"
    "QVRYheWYvW4bRxDH/7NckpIQGKBgGAhUygiUIk1cOA9gwKU7w5VfwG+RPqUfwKVbl4YfIG5U"
    "pBIgqAwCGDL1AYo83u3OpFjece92b+8o0QYSLRaL2eFwb+bHmbk7kogAEBEuisV0en5ycvbp"
    "09+fP09PT7PLS2sMRHBPBtFA653JZP/x44OnTw+fPXt4dLS7v6+GQyICQPPz87/ev//z7dvL"
    "szOyVu/tjR880Lu7g9GIiEC0LT8ANM+qDveuQjElIh/XbCg0ptqhbRcNXRJmLop8Nsu+fl1e"
    "XzPR5PDwtzdvfnn1iv44OJh9+SLW0niMnR0AyHNkmTBHfN1whJFHNNEAYuH1Oi2hSZqtlURQ"
    "CuMxhkMCiuvr4uICRD88ekS/DwbMDBEmEiIASoQ2Lb0NPbt1SKkgt+qGEDGRiJibGzufQ0Qp"
    "pSHiupIqhb7j1gF8L7Ibn+ZpCFAAW2vyfIVIRKP/aC+c7QbZUZjf4EeKng8Rs1yubnFEADRc"
    "F0/k1BZTo6fZdy+66PnFYlHM56h6N1Ess+4rHT+n8pubfDYTa+GNdc9afS9M0W8MaLun3b3B"
    "2TzPrq6sa1XVaPYsR6oB6B7QqZQmy5ZXV7Yo2pqS9r9JSv0nouqp6WUmYpbLfDYzWdb5MFDv"
    "WX4Z/v/oACByL3ZmsTCLxaYvc11luKG7m7W8alupYvGvzqmU/kf1C4gbzMIs1rK1bIybwnz3"
    "l9zy0SFWhp101l6LgFmYmRluOmUVkQgBguBdzH27h6OSlMXbhpotDCIQlXdDhywoQ3IQncKF"
    "ai0bI9YSM7zgqZTDNSpEt+GQ0qwNgQQr6sgaBv5sHJX0w90NHSNAEWmlyAO0EkTYGM5zMDul"
    "LgPwAXWS8rmEjBqaMIBoZoWkQkZRodpyoGygLP3zMouIRkQjojUmQBwja5WHpgEoKifWKJq2"
    "/JKWbRuszmxq4+KzayjZKf3nLAKGROMSlhQFL5dg1gGmEFCC111gJZIrnVCJVAoNOLb1pwHy"
    "0qaERTR0mSXCy6UUhRaJYupk14aprYu18UqnVVtOIYDCMX1VfdFU8rcE5OWl1z1rSDQU4SxT"
    "xpBHKqzBzkTrU5ttjMIRrb4+LalnoUUZcZ1X2bPKMRAZ5PnAI9XAFFJTASBVh9InxRLIEk0q"
    "XX2JmYDCMcF67unqn7+BtbokpQIcoRBO1ZVlCPAlSEUZ3Q5QG5RK5vatcR5Wjw7uT2RlrfZI"
    "qQBQW6L1qdPOkmwgk0COcomSSuROHzpS3QEBBgaNMhQAIspaHQOk6oDaCDbMeva4KK8oqVvQ"
    "6cnFX6lsUqrU+I1F//zy5enHj/nFxZR5F9j1iETRqABcFF8CImLgfF7RDtWnrKLxh6sTVMmi"
    "Wqm+ZSAHLoFCKT2Z/PT8OTFzNp2efPhw/O7dP8fHxXyumUfACND1eDqLqLOXR+37DAkI+ijb"
    "+KaNQwM3DVAAOcBK6b29H588+fX166MXL3Ymk38BAbsi18hyJgEAAAAASUVORK5CYII=")
getbutton_redData = button_red.GetData
getbutton_redImage = button_red.GetImage
getbutton_redBitmap = button_red.GetBitmap

class StaticBmpButton(wx.PyControl):

    def __init__(self, parent, width, height, bmpNormal, bmpHover, bmpClick, bmpOff, id=wx.ID_ANY, label="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.NO_BORDER, validator=wx.DefaultValidator, name="StaticBmpButton"):

        wx.PyControl.__init__(self, parent=parent, id=id, pos=pos, size=size, style=wx.NO_BORDER, validator=validator, name=name)

        self.width = width
        self.height = height
        self.bmpNormal = bmpNormal
        self.bmpHover = bmpHover
        self.bmpClick = bmpClick
        self.bmpOff = bmpOff
        self._InitBuffer()
        self._InitPaintObjects()
        self._BindEvents()
        self._SetProperties(label)
        self.SetInitialSize(size)
        self.InheritAttributes()

    def _SetProperties(self, label):
        self._label = label
        self._leftButtonDown = False
        self.SetLabelColor(wx.WHITE)
        self.SetCursor(wx.StockCursor(wx.CURSOR_HAND))
        self.SetFont(wx.Font(pointSize=10, family=wx.FONTFAMILY_SWISS, style=wx.FONTSTYLE_NORMAL, weight=wx.FONTWEIGHT_NORMAL))
        self._hasFocus = False

    def DoGetBestSize(self):
        best = wx.Size(self.width, self.height)
        i = self.height * 0.577
        self.ratio = int(i)
        self.CacheBestSize(best)
        return best

    def GetLabel(self):
        return self._label

    def SetLabelColor(self, color):
        self._labelColor = color

    def _InitPaintObjects(self):
        self._pen = wx.Pen(wx.BLACK, 2, wx.SOLID)
        self._brush = wx.Brush(wx.BLACK, wx.SOLID)

    def _BindEvents(self):
        self.Bind(wx.EVT_ERASE_BACKGROUND, self._OnEraseBackground)
        self.Bind(wx.EVT_SIZE, self._OnSize)
        self.Bind(wx.EVT_PAINT, self._OnPaint)
        self.Bind(wx.EVT_LEFT_DOWN, self._OnMouseClickDown)
        self.Bind(wx.EVT_ENTER_WINDOW, self._OnMouseHover)
        self.Bind(wx.EVT_LEFT_UP, self._OnMouseClickUp)
        self.Bind(wx.EVT_LEAVE_WINDOW, self._OnLeaveWindow)
        self.Bind(wx.EVT_LEFT_DCLICK, self._OnMouseClickDown)

    def Enable(self, enable=True):
        wx.PyControl.Enable(self, enable)
        self.Refresh()

    def _OnLeaveWindow(self, evt):
        self._leftButtonDown = False
        self._hasFocus = False
        self.Refresh()

    def _OnMouseHover(self, evt):
        if self.IsEnabled():
            self._hasFocus = True
            self.Refresh()

    def _OnMouseClickDown(self, evt):
        if self.IsEnabled():
            self._leftButtonDown = True
            self.Refresh()

    def _OnMouseClickUp(self, evt):
        if self.IsEnabled():
            buttonEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, self.GetId())
            buttonEvent.SetEventObject(self)
            self.GetEventHandler().ProcessEvent(buttonEvent)
            self._leftButtonDown = False
            self.Refresh()

    def _OnPaint(self, evt):
        self._Draw(wx.BufferedPaintDC(self, self._buffer))

    def _OnSize(self, evt):
        self._InitBuffer()
        self.Refresh()
        evt.Skip()

    def _OnEraseBackground(self, evt):
        pass

    def _InitBuffer(self):
        self._buffer = wx.EmptyBitmap(self.width, self.height)

    def _Draw(self, dc):
        dc = wx.GCDC(dc)
        self._DrawBox(dc)
        self._DrawLabel(dc)

    def _DrawLabel(self, dc):
        dc.SetFont(self.GetFont())
        tw, th = dc.GetTextExtent(self._label)
        tposx, tposy = ((self.width/2)-(tw/2), (self.height/2)-(th/2))

        dc.SetTextForeground(wx.Color(0,0,0,75))
        dc.DrawText(self._label, tposx+1, tposy+1)

        if self.IsEnabled():
            dc.SetTextForeground(self._labelColor)
        else:
            dc.SetTextForeground(wx.Color(255,255,255,75))

        dc.DrawText(self._label, tposx, tposy)

    def _DrawBox(self, dc):
        if self.IsEnabled():
            if self._leftButtonDown:
                dc.DrawBitmap(self.bmpClick, 0, 0, True)
            elif self._hasFocus:
                dc.DrawBitmap(self.bmpHover, 0, 0, True)
            else:
                dc.DrawBitmap(self.bmpNormal, 0, 0, True)
        else:
            dc.DrawBitmap(self.bmpOff, 0, 0, True)

class _DemoWindow(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, -1, size=(640,480))

        self.SetBackgroundColour(wx.Color(255,255,255))

        bmpNormal = getbutton_blueBitmap()
        bmpHover = getbutton_green_brightBitmap()
        bmpClick = getbutton_redBitmap()
        bmpOff = getbutton_grayBitmap()
        self.test_button1 = StaticBmpButton(self, 100, 26, bmpNormal, bmpHover, bmpClick, bmpOff, -1, label="Hello")
        self.test_button2 = StaticBmpButton(self, 100, 26, bmpNormal, bmpHover, bmpClick, bmpOff, -1, label="Connect")
        self.test_button3 = StaticBmpButton(self, 100, 26, bmpNormal, bmpHover, bmpClick, bmpOff, -1, label="Disconnect")

        self.Bind(wx.EVT_BUTTON, self.OnButton1, self.test_button1)
        self.Bind(wx.EVT_BUTTON, self.OnButton2, self.test_button2)
        self.Bind(wx.EVT_BUTTON, self.OnCloseButton, self.test_button3)

        self.test_button3.Disable()

        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(self.test_button1, 0, wx.EXPAND|wx.ALL, 10)
        hbox.Add(self.test_button2, 0, wx.EXPAND|wx.ALL, 10)
        hbox.Add(self.test_button3, 0, wx.EXPAND|wx.ALL, 10)
        vbox.AddStretchSpacer(1)
        vbox.Add(hbox, 0, wx.ALIGN_CENTER)
        vbox.AddStretchSpacer(1)
        self.SetSizer(vbox)
        self.Fit()

        self.SetSize((640, 480))

    def OnButton1(self, evt):
        print evt.GetEventObject().GetLabel()
        self.test_button2.Enable()
        self.test_button3.Enable()

    def OnButton2(self, evt):
        self.test_button2.Disable()
        self.test_button3.Enable()

    def OnCloseButton(self, evt):
        self.test_button3.Disable()
        self.test_button2.Enable()

def _demo():
    app = wx.PySimpleApp()
    _DemoWindow().Show()
    app.MainLoop()

if __name__ == '__main__':
    _demo()

答案 1 :(得分:0)

感谢GolfDop​​e的评论和解决方案。但我正在寻找的是一种使用wxPython的内置小部件的方法。

在搜索了大量的问题和答案之后,看起来我需要的是GenStaticBitmap。它看起来像这样,并做我需要的。如果它证明不够强大,我将被迫用上面的PyControl做一些事情。 这似乎有用:

import  wx.lib.statbmp as gen_statbmp

#  (gen_statbmp.GenStaticBitmap).__init__(self, parent, ID, bitmap, pos, size, style, name)
########################################################################
class MyBitmapButton(gen_statbmp.GenStaticBitmap):
    """Subclasses a GenStaticBitmap and adds button like functionality 
        requires the three bitmap images needed for on,off and hover"""

    #----------------------------------------------------------------------
    def __init__(self, parent, ID, bmpoff,bmpon,bmphover):
        """Constructor"""
        gen_statbmp.GenStaticBitmap.__init__(self, parent, ID, bmpoff)
        self.bmpoff=bmpoff
        self.bmpon=bmpon
        self.bmphover=bmphover
        self.SetBitmap(bmpoff)
        self.state='off'
        # Bind Mouse Actions to button
        self.Bind(wx.EVT_ENTER_WINDOW,self.On_Hover)
        self.Bind(wx.EVT_LEAVE_WINDOW,self.On_Leave)
        self.Bind(wx.EVT_LEFT_DOWN,self.On_Click)

    # Define Mouse Actions and Button Response
    def On_Hover(self,event):
        self.SetBitmap(self.bmphover)

    def On_Leave(self,event):
        #print 'on enter button state is',self.state
        if self.state=='on':
            self.SetBitmap(self.bmpon)
            self.state='on'
        if self.state=='off':
            self.SetBitmap(self.bmpoff)
            self.state='off'
        #print 'on leave button state is',self.state

    def On_Click(self,event):
        self.SetBitmap(self.bmpon)
        event.Skip()
########################################################################