How do I get the scroll position / range from a wx.TextCtrl control in wxPython

风格不统一 提交于 2019-12-02 03:54:02

I just tested a simple example (checking GetScrollPos(0) and GetScrollRange(0) in EVT_TEXT event handler for wx.TextCtrl) and it works fine for me - they return index of currently shown line and total number of lines, respectively.

Maybe the problem is your wxPython version? I used:

>>> import wx
>>> wx.version()
'2.8.9.1 (msw-unicode)'

Okay, so here's where I've got to:

import wx
from threading import Timer
import time

class Form1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.logger = wx.TextCtrl(self,5, "",wx.Point(20,20), wx.Size(200,200), \
                wx.TE_MULTILINE |  wx.TE_READONLY)# |  wx.TE_RICH2)

        t = Timer(0.1, self.AddText)
        t.start()

    def AddText(self):
        # Resart the timer
        t = Timer(0.25, self.AddText)
        t.start() 

        # Work out if we're at the end of the file
        currentCaretPosition = self.logger.GetInsertionPoint()
        currentLengthOfText = self.logger.GetLastPosition()
        if currentCaretPosition != currentLengthOfText:
            self.holdingBack = True
        else:
            self.holdingBack = False

        timeStamp = str(time.time())

        # If we're not at the end of the file, we're holding back
        if self.holdingBack:
            print "%s FROZEN"%(timeStamp)
            self.logger.Freeze()
            (currentSelectionStart, currentSelectionEnd) = self.logger.GetSelection()
            self.logger.AppendText(timeStamp+"\n")
            self.logger.SetInsertionPoint(currentCaretPosition)
            self.logger.SetSelection(currentSelectionStart, currentSelectionEnd)
            self.logger.Thaw()
        else:
            print "%s THAWED"%(timeStamp)
            self.logger.AppendText(timeStamp+"\n")

app = wx.PySimpleApp()
frame = wx.Frame(None, size=(550,425))
Form1(frame)
frame.Show(1)
app.MainLoop()

This simple demo app works almost perfectly. It scrolls neatly unless the user clicks a line which isn't at the end of the text. Thereafter it stays nice and still so you can select text (note: there's still a bug there in that if you select up not down it clears your selection).

The biggest annoyance is that if I try and enable the "| wx.TE_RICH2" option, it all goes a bit pear-shaped. I really need this to do syntax highlighting of errors, but if I can't enable that option, I'm doomed to monochrome - boo!

Any more ideas on how to hold back scrolling on the rich edit control?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!