fixing some text in wx.textctrl

25 views
Skip to first unread message

Hemadri Saxena

unread,
Aug 4, 2014, 3:17:19 PM8/4/14
to wxpytho...@googlegroups.com
I am trying to create a command terminal like window..I have used the following code..Problem is that it set the command prompt as ">>>" that is editable.

I want window with fixed ">>>" with cursor waiting for command...now the issue is that user can easily delete ">>>"  as it is a simple text..

Is it any way to fixed this par of text in wx.textctrl or in any other control.

class MyFrame(wx.Frame):
   
    def __init__(self, parent, title):
           wx.Frame.__init__(self, parent, title=title, size=(400,400))
           self.SetPosition(wx.Point(0,0))                           
           self.cmdArea = wx.TextCtrl(self, style=wx.TE_MULTILINE)          
           self.cmdArea.SetValue(">>>")                
           self.Show(True)
           

Nathan McCorkle

unread,
Aug 4, 2014, 5:25:25 PM8/4/14
to wxpytho...@googlegroups.com
You might be able to Bind the EVT_TEXT event, check if the current key is backspace, and Veto the event if the cursor position is <=4 (i.e. the 4th character, since you want 3 > characters). This will fail if the user types a very long line which starts a new line, then tries to backspace on the newline that they just typed. In that case, you'll likely want to save the row/line number just after you print >>>, then compare the position during the EVT_TEXT events with that one.

Nathan McCorkle

unread,
Aug 4, 2014, 5:48:27 PM8/4/14
to wxpytho...@googlegroups.com
Here's some basic code to get you going... you will need to update self.lastPrompt yourself... I guess some time after an EVT_TEXT_ENTER is pressed... or best after you print a new >>> prompt:

       t3 = wx.TextCtrl(self, -1,
                        "Here is a looooooooooooooong line of text set in the control.\n\n"
                        "The quick brown fox jumped over the lazy dog...>>>",
                       size=(200, 100), style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)

        t3.SetInsertionPoint(t3.GetLastPosition())
        self.lastPrompt = t3.GetLastPosition()
        self.Bind(wx.EVT_TEXT, self.EvtText, t3)
        self.Bind(wx.EVT_TEXT_ENTER, self.EvtTextEnter, t3)
        t3.Bind(wx.EVT_CHAR, self.OnChar)


    def OnChar(self, event):
        #disable backspace for positions less than or equal to the last char of >>>
        if event.GetEventObject().GetInsertionPoint() <= self.lastPrompt and event.GetKeyCode() == 8:
            return False
        #disable entering text only before the >>>, text can be entered at/after this 
        elif event.GetEventObject().GetInsertionPoint() < self.lastPrompt:
            return False
        #do default behaviour for all other cases
        else:
            event.Skip()
Reply all
Reply to author
Forward
0 new messages