在TextCtrl中禁用光标 - wxPython

时间:2014-07-02 18:05:20

标签: python wxpython

我的代码如下

log = wx.TextCtrl(面板,wx.ID_ANY,尺寸=(300,100),                           style = wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL | wx.TE_DONTWRAP)

我正在通过重定向stdout将日志写入此框。 如何使光标消失为TextCtrl,因为它现在根据光标的位置附加日志。我不想给用户提供将光标放在框中特定位置的特权

5 个答案:

答案 0 :(得分:2)

ummmm

log.Enable(False) 

我想......

或者如果你觉得很棒,你也可以这样做

log.Disable()

答案 1 :(得分:0)

我自己就碰到了这个。我解决了这个问题,同时保留了突出显示条目的功能,并通过在我的UI子类中定义内部打印功能来使用滚动条。

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()

从技术上讲,我认为运行时间比仅禁用wx.TextCtrl要慢一点,但我看不出这实际上是一个问题。

答案 2 :(得分:0)

但是如果您使用log.Disable,则滚动条无法正常工作

答案 3 :(得分:0)

对于Windows用户,您可以致电

wx.CallAfter(self.your_text_ctrl.HideNativeCaret)

您必须在更新文本

时随时拨打电话

答案 4 :(得分:0)

这对我有用。

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

def onfocus(self,e):
    self.log.SetInsertionPointEnd()
相关问题