sarath joseph
sarath joseph

Reputation: 2579

Disable cursor in TextCtrl - wxPython

I have code as follows

log = wx.TextCtrl(panel, wx.ID_ANY, size=(300,100), style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL|wx.TE_DONTWRAP)

I'm writing logs into this box by redirecting stdout. How do I make the cursor dissappear for the TextCtrl because it appends logs based on the position of the cursor right now. I dont want to give the user the privilage to place the cursor at a particular spot in the box basically

Upvotes: 1

Views: 3963

Answers (5)

vishal
vishal

Reputation: 11

This worked for me.

self.log.Bind(wx.EVT_MOUSE_EVENTS,self.onfocus)

def onfocus(self,e):
    self.log.SetInsertionPointEnd()

Upvotes: 1

user2682863
user2682863

Reputation: 3217

For windows users you can call

wx.CallAfter(self.your_text_ctrl.HideNativeCaret)

You'll have to call it anytime you update the text

Upvotes: 0

TreeCatCat
TreeCatCat

Reputation: 143

But if you use log.Disable, the scrollbar won't work

Upvotes: 1

Allan B
Allan B

Reputation: 347

I just ran into this myself. I resolved it while retaining the ability to highlight entries and use the scrollbar by defining an internal print function in my UI subclass.

import wx,sys

class RedirectText(object):
    def __init__(self,wxTextCtrl):
        self.out=wxTextCtrl
    def write(self,string):
        self.out.WriteText(string)

class SomeGUI(wx.Frame):
    def __init__(self,parent,title):
        super(SomeGUI,self).__init__(parent,title=title)

        self.mainsizer=wx.GridBagSizer(2,2)
        self.textout=wx.TextCtrl(self,size=(-1,80), style=wx.TE_MULTILINE|wx.TE_READONLY)
        self.mainsizer.Add(self.textout,(0,0),span=wx.GBSpan(1,3),flag=wx.EXPAND)

        self.redir=RedirectText(self.textout)
        sys.stdout=self.redir

        self.buttons=[wx.Button(self, label=val) for val in ['Bob Dole', 'Batman', 'Pet Rock']]
        for i,v in enumerate(self.buttons):
            self.mainsizer.Add(v,(1,i),flag=wx.EXPAND)

        self.Bind(wx.EVT_BUTTON,self.BobDole,self.buttons[0])
        self.Bind(wx.EVT_BUTTON,self.Batman,self.buttons[1])
        self.Bind(wx.EVT_BUTTON,self.Rock,self.buttons[2])

        self.SetSizerAndFit(self.mainsizer)
        self.Show()

    def iprint(self,string): #iprint because "internal print", not because Apple
        self.textout.SetInsertionPointEnd()
        print(string)

    def BobDole(self,e):
        self.iprint('Bob Dole!')

    def Batman(self,e):
        self.iprint('The hero we deserve.')

    def Rock(self,e):
        self.iprint('It looks happy...')

def main():
    app=wx.App()
    app.locale=wx.Locale(wx.LANGUAGE_ENGLISH)
    somegui=SomeGUI(None, title='It prints text and stuff')
    app.MainLoop()

if __name__=='__main__':
    main()

Technically speaking, I'd imagine the running time is marginally slower than just disabling the wx.TextCtrl, but I can't see that realistically becoming an issue.

Upvotes: 2

Joran Beasley
Joran Beasley

Reputation: 113948

ummmm

log.Enable(False) 

?

I think ...

or if you feel awesome you could also do

log.Disable()

Upvotes: 2

Related Questions