user1401950
user1401950

Reputation: 995

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 or ListBox.

Here's my code:

import wx
class ExtraDataForTxtCtrl(wx.TextCtrl):

    def __init(self, ExtraTextData):
        self.ExtraTextData=ExtraTextData


    def getExtraTCData(self):
        return self.ExtraTextData

    def setExtraTCData(self, ExtraTextData):
        self.ExtraTextData=ExtraTextData

My problem is that I'm new to python and have no idea how to implement this and if it is correct or not.

Upvotes: 1

Views: 691

Answers (2)

user1401950
user1401950

Reputation: 995

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:

class TextDataHolder:
    def __init__(self, wxTextControl, data):

        self.wxTextControl=wxTextControl
        self.data=data

    def setDataTxt(self,data):
        self.wxTextControl=wxTextControl
        self.data=data

    def getDataTxt(self):
        return self.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?'  

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 114018

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

Upvotes: 2

Related Questions