Reputation: 1190
I'd like to put a prompt (italic, grey, unchangeable text) into an input field, which is removed when the user types the first character (or the field is focussed, which would not be as nice).
The rationale is to give the user some hint about the meaning of the field as long as it's empty.
Documentation at https://wxpython.org/Phoenix/docs/html/wx.TextCtrl.html says that text styles are (only?) available for multi-line fields, which I do not need.
Can this be achieved with the wxpython TextCtrl? How?
Many thanks!
Upvotes: 1
Views: 563
Reputation: 149
wx.TextCtrl is a subclass of wx.TextEntry, which has SetHint() method. It does not do exactly what you want: prompt is removed when the field is focused, text is not italic (at least on Linux). But it is simple and consistent with other similar stuff on the system.
Upvotes: 0
Reputation: 22448
One simple way is to set the Font
to have the attributes that you require and then reset them when the "Hint" text is backspaced.
i.e.
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Test TextCtrl')
self.panel = wx.Panel(self)
self.text1 = wx.TextCtrl(self.panel,value="Normal Text",pos=(10,10),size=(350,30))
self.text2 = wx.TextCtrl(self.panel,value="Italic greyed hint text",pos=(10,40),size=(350,30))
font = wx.Font(10, wx.DEFAULT, wx.FONTSTYLE_ITALIC, wx.NORMAL)
self.text2.SetFont(font)
self.text2.SetForegroundColour('#848484')
self.text2.Bind(wx.EVT_TEXT, self.On_Text_Active)
self.Show()
def On_Text_Active(self, event):
if self.text2.GetForegroundColour() == '#848484':
font = wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
self.text2.SetFont(font)
self.text2.SetForegroundColour(wx.BLACK)
self.text2.SetValue('')
if __name__ == '__main__':
app = wx.App()
frame = MainFrame()
app.MainLoop()
Upvotes: 1