Creating subclass for wx.TextCtrl

后端 未结 2 1359
暖寄归人
暖寄归人 2021-01-24 09:51

I\'m creating a subclass for the wx.TextCtrl in wxpython.

I want this class to add extra data to the wx.TextCtrl widgets similar as to the way extra data can be added to

2条回答
  •  终归单人心
    2021-01-24 10:07

    import wx
    class ExtraDataForTxtCtrl(wx.TextCtrl):
    
        def __init__(self,*args,**kwargs):
            self.ExtraTextData=kwargs.pop("ExtraTextData")
            wx.TextCtrl.__init__(self,*args,**kwargs)
    
    
        def getExtraTCData(self):
            return self.ExtraTextData
    
        def setExtraTCData(self, ExtraTextData):
            self.ExtraTextData=ExtraTextData
    

    possibly a better solution would be to use set/getattr

    class DataTxtCtrl(wx.TextCtrl):
    
        def __init__(self,*args,**kwargs):
            self.datadict = {}
            self.ExtraTextData=kwargs.pop("ExtraTextData")
            wx.TextCtrl.__init__(self,*args,**kwargs)
        def __getattr__(self,attr):
            return self.datadict[attr]
        def __setattr__(self,attr,val):
            self.datadict[attr]=val
    

    then you can set many variables and use it like normal

       a = wx.App(redirect=False)
       f = wx.Dialog(None,-1,"Example")
       te = DataTxtCtrl(f,-1,"some_default")
       te.somevar = "hello"
       te.someother = "world"
       print te.somevar+" "+te.someothervar
       f.ShowModal()
    

提交回复
热议问题