How to assign

Laszlo Zsolt Nagy gandalf at geochemsource.com
Wed Jan 26 08:39:42 CET 2005


Jan Rienyer Gadil wrote:

>Sort of a newbie question:
>How am i going to assign to a variable anything the user inputs on a wxTxtCtrl?
>  
>
I'm affraid you have to do it manually. I think the best solution is to 
use a property like this:

import wx

class  FrameMain(wx.Frame):
   def __init__(self, *args,**kwargs):
       DEFAULT_VALUE = "Some string here"
       wx.Frame.__init__(self, *args,**kwargs)
       self.txt = wx.TextCtrl(self,value=DEFAULT_VALUE)
       self._value = DEFAULT_VALUE
       self._txt_updating = False
       self.txt.Bind(
           wx.EVT_TEXT,
           self.TextChanged
       )
       self.value = "Test value" # This will set the property and the 
text control too
   def TextChanged(self,event):
       self._value = self.txt.GetValue()
       print "Property changed to %s" % self._value
   def _GetTextValue(self):
       return self._value
   def _SetTextValue(self,value):
       if not self._txt_updating:
           self._txt_updating = True
           try:
               self.txt.SetValue(value)
           finally:
               self._txt_updating = False
   value = property(_GetTextValue,_SetTextValue,'Your variable')
 
app = wx.PySimpleApp()
frame = FrameMain(None, -1, "TextValue")
frame.Show(True)
app.MainLoop()

In this example, the frame has a property called "value" and that has 
the same cached value as the text in the textcontrol.




More information about the Python-list mailing list