Skip to content Skip to sidebar Skip to footer

Creating Subclass For Wx.textctrl

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 a ComboBox

Solution 1:

import wx
classExtraDataForTxtCtrl(wx.TextCtrl):def__init__(self,*args,**kwargs):
        self.ExtraTextData=kwargs.pop("ExtraTextData")
        wx.TextCtrl.__init__(self,*args,**kwargs)


    defgetExtraTCData(self):
        returnself.ExtraTextData

    defsetExtraTCData(self, ExtraTextData):
        self.ExtraTextData=ExtraTextData

possibly a better solution would be to use set/getattr

classDataTxtCtrl(wx.TextCtrl):def__init__(self,*args,**kwargs):
        self.datadict = {}
        self.ExtraTextData=kwargs.pop("ExtraTextData")
        wx.TextCtrl.__init__(self,*args,**kwargs)
    def__getattr__(self,attr):
        returnself.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()

Solution 2:

Instead of creating a subclass I just decided to create my own class which links an extra string value to wx.textCtrl widgets.

Thanks to all who contributed! :)

Heres my code:

classTextDataHolder:def__init__(self, wxTextControl, data):

        self.wxTextControl=wxTextControl
        self.data=data

    defsetDataTxt(self,data):
        self.wxTextControl=wxTextControl
        self.data=data

    defgetDataTxt(self):
        returnself.data

Heres how I implemented it:

import wx, TextDataHolder

exampleCtrl=wx.TextCtrl(self, -1, "Hello")
exampleData=TextDataHolder.TextDataHolder(exampleCtrl,"Sup?")
print exampleData.getDataTxt() #prints 'Sup?'  

Post a Comment for "Creating Subclass For Wx.textctrl"