[ComboBox]: set default value on text area

657 views
Skip to first unread message

artwood

unread,
May 6, 2011, 4:33:14 AM5/6/11
to wxPython-users
Hi! I wish to have a default value on the text area of combobox to
help users to understand what to do. For help you to understand what I
mean take a look at this example: http://dl.dropbox.com/u/8044435/login.jpg

Robin Dunn

unread,
May 6, 2011, 2:19:49 PM5/6/11
to wxpytho...@googlegroups.com

There isn't anything built-in for that, but it's not hard to implement.
Just use SetValue to set your message when the widget is created.
You'll also want to compare the value with that message when you later
get the value from the widget, and if it is the same as the message then
treat it as if it was empty. For extra niceness you can watch for when
the user clears any value that they've started typing and then reset the
widget's value to the starting message again, and it's also nice to
change the foreground colour to a medium grey when the message is displayed.


--
Robin Dunn
Software Craftsman
http://wxPython.org

ozan hacıbekiroğlu

unread,
May 7, 2011, 2:54:40 AM5/7/11
to wxpytho...@googlegroups.com
This may be helpful;

class MyTextCtrl(wx.TextCtrl):
    def __init__(self, parent, ID = -1, value = wx.EmptyString, pos = wx.DefaultPosition,
                 size = wx.DefaultSize, style = 0, validator = wx.DefaultValidator):
        super(MyTextCtrl, self).__init__(parent, ID, value = value, pos = pos, size = size, style = style,
                                        validator = validator)
        self.empty_value = value
        self.default_foreground_color = self.GetForegroundColour()
        self.Bind(wx.EVT_SET_FOCUS, self.on_focus)
        self.Bind(wx.EVT_KILL_FOCUS, self.on_leave)
        self.SetForegroundColour(wx.Color(84, 84, 84))

    def GetValue(self):
        val = super(MyTextCtrl, self).GetValue()
        return "" if val == self.empty_value else val

    def GetValue_(self):
        return super(MyTextCtrl, self).GetValue()

    def on_focus(self, evt):
        if self.empty_value == self.GetValue_():
            self.SetForegroundColour(self.default_foreground_color)
            self.ChangeValue("")

    def on_leave(self, evt):
        if self.empty_value == self.GetValue_() or self.GetValue_() == "":
            self.SetForegroundColour(wx.Color(84, 84, 84))
            self.ChangeValue(self.empty_value)


For example:

txt_box = MyTextCtrl(self, -1, value = "Username")



Ozan HACIBEKIROGLU

Reply all
Reply to author
Forward
0 new messages