wxpython textctrl添加滚动条_如何从wxPython中的wx.TextCtrl控件获取滚动位置/范围

该博客介绍了如何在wxPython中为TextCtrl添加滚动条,并探讨了在尝试启用wx.TE_RICH2选项进行富文本语法高亮时遇到的问题。当用户点击非末尾行时,滚动条管理会失效,但可以冻结和解冻TextCtrl来保持选中状态。同时,启用wx.TE_RICH2导致滚动条行为异常。
摘要由CSDN通过智能技术生成

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值