Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wxPython: How to clear default text in TextCtrl with one click

I am making a GUI using wxPython, and I have a text box that requires user input:

self.boxQuantity = wx.TextCtrl(panel, value="Enter quantity", pos=(100, 150), size=(100, 30))
self.Bind(wx.EVT_TEXT, self.getQuantity, self.boxQuantity)

I want the user to be able to click on the text box, and "Enter quantity" to disappear immediately, instead of having to use backspace. Is this possible?

I'm using Windows 10, Python 2.7.9.

like image 485
Kaya311 Avatar asked Dec 20 '15 08:12

Kaya311


People also ask

What is textctrl in wxPython?

In wxPython, an object of wx.TextCtrl class serves this purpose. It is a control in which the text can be displayed and edited. The TextCtrl widget can be a single line, multi-line or a password field. TextCtrl class constructor takes the following form − wx.TextCtrl(parent, id, value, pos, size, style)

What is a text control in WX?

A text control allows text to be displayed and edited. It may be single line or multi-line. Notice that a lot of methods of the text controls are found in the base wx.TextEntry class which is a common base class for wx.TextCtrl and other controls using a single line text entry field (e.g. wx.ComboBox ).

What are the different types of text boxes in WX textctrl?

In the following example, four objects of wx.TextCtrl class with different attributes are placed on the panel. While the first is a normal text box, the second is a password field. The third one is a multiline text box and the last text box is non-editable.

How to remove the vertical scrollbar from the textctrl widget?

We have applied two Styles, wx.TE_MULTILINE, which makes the widget go from single line mode to multiple, and the wx.TE_NO_VSCROLL style which removes the vertical scrollbar which appears by default on Multiline TextCtrl widgets. We’ve also created a function, OnPress (), which prints out the text in the TextCtrl Widget.


2 Answers

I know that this question is old, but wxPython now (4.1) has a new SetHint function for a TextCtrl:

text = wx.TextCtrl(self)
text.SetHint('First name')  # This text is grey, and disappears when you type
like image 54
Legorooj Avatar answered Oct 18 '22 01:10

Legorooj


I think you want

def toggle1(evt):
    if self.boxQuantity.GetValue() == "Enter quantity":
        self.boxQuantity.SetValue("")
    evt.Skip()
def toggle2(evt):
    if self.boxQuantity.GetValue() == "":
        self.boxQuantity.SetValue("Enter quantity")
    evt.Skip()    

self.boxQuantity.Bind(wx.EVT_FOCUS,toggle1)
self.boxQuantity.Bind(wx.EVT_KILL_FOCUS,toggle2)

its probably better to create a subclass

import wx
class PlaceholderTextCtrl(wx.TextCtrl):
    def __init__(self, *args, **kwargs):
        self.default_text = kwargs.pop("placeholder", "")
        wx.TextCtrl.__init__(self, *args, **kwargs)
        self.OnKillFocus(None)
        self.Bind(wx.EVT_SET_FOCUS, self.OnFocus)
        self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)

    def OnFocus(self, evt):
        self.SetForegroundColour(wx.BLACK)
        if self.GetValue() == self.default_text:
            self.SetValue("")
        evt.Skip()

    def OnKillFocus(self, evt):
        if self.GetValue().strip() == "":
            self.SetValue(self.default_text)
            self.SetForegroundColour(wx.LIGHT_GREY)
        if evt:
            evt.Skip()

# then sometime later...

self.text_entry1 = PlaceHolderTextCtrl(self,-1,placeholder="Enter Value")

something like that at least ...

like image 28
Joran Beasley Avatar answered Oct 17 '22 23:10

Joran Beasley