如何从 wxPython 中的 wx.TextCtrl 控件获取滚动位置/范围

发布于 2024-07-06 02:10:47 字数 529 浏览 10 评论 0原文

我有一个小型日志记录应用程序(用 wxPython 编写),它从我们正在开发的一些套件接收数据,并且我想在滚动窗口中显示文本。 就目前情况而言,我使用 wx.TextCtrl 进行文本显示,但滚动行为存在一些问题。

基本上,我希望如果滚动条位于窗口底部(即传入数据的末尾),添加更多数据应该向前滚动视图。 但是,如果视图已向上滚动一点(即用户正在查看一些有趣的内容,例如错误消息),则应用程序应该仅在末尾添加文本而不再滚动。

我现在有两个问题:

  1. 我不知道如何检索当前滚动位置(调用 GetScrollPos() 似乎不起作用 - 它们只是返回 0)。
  2. 我不知道如何检索滚动条的当前范围(调用 GetScrollRange() 仅返回 1)。

我用谷歌搜索了一下,似乎有一些提示表明 GetScrollPos 和 GetScrollRange 对于 wx.TextCtrl 不起作用? 还有其他人有这方面的经验吗? 有没有一个很好的简单方法来解决这个问题,或者我是否必须推出自己的 wx.TextCtrl?

I have a little logging app (written in wxPython) that receives data from a bit of kit we're developing, and I want to display the text in a scrolling window. As it stands I'm using a wx.TextCtrl for the text display, but I'm having some issues with the scrolling behaviour.

Basically, I'd like it so that if the scrollbar is at the bottom of the window (i.e. the end of the incoming data), adding more data should scroll the view onwards. If, however the view has been scrolled up a little (i.e. the user is looking at something interesting like an error message), the app should just add the text on the end without scrolling any more.

I've got two problems at the moment:

  1. I can't work out how to retrieve the current scroll position (calls to GetScrollPos() don't seem to work - they just return 0).
  2. I can't work out how to retrieve the current range of the scroll bar (calls to GetScrollRange() just return 1).

I've googled a bit and there seem to be a few hints that suggest GetScrollPos and GetScrollRange won't work for a wx.TextCtrl? Has anyone else had any experience in this area? Is there a nice easy way to solve the problem or am I going to have to roll my own wx.TextCtrl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情 2024-07-13 02:10:47

好的,这就是我要做的:

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

这个简单的演示应用程序几乎可以完美运行。 除非用户单击不在文本末尾的行,否则它会整齐地滚动。 此后,它保持良好状态,以便您可以选择文本(注意:仍然存在一个错误,如果您选择向上而不是向下,它会清除您的选择)。

最大的烦恼是,如果我尝试启用“| wx.TE_RICH2”选项,一切都会变得有点梨形。 我确实需要这个来对错误进行语法突出显示,但如果我无法启用该选项,我注定只能使用单色 - 嘘!

关于如何阻止在丰富的编辑控件上滚动还有更多想法吗?

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?

倾城月光淡如水﹏ 2024-07-13 02:10:47

我刚刚测试了一个简单的示例(在 wx.TextCtrl< 的 EVT_TEXT 事件处理程序中检查 GetScrollPos(0)GetScrollRange(0) /code>),它对我来说工作得很好 - 它们分别返回当前显示行的索引和总行数。

也许问题是你的wxPython版本? 我用了:

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

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)'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文