如何从 wxPython 中的 wx.TextCtrl 控件获取滚动位置/范围
我有一个小型日志记录应用程序(用 wxPython 编写),它从我们正在开发的一些套件接收数据,并且我想在滚动窗口中显示文本。 就目前情况而言,我使用 wx.TextCtrl 进行文本显示,但滚动行为存在一些问题。
基本上,我希望如果滚动条位于窗口底部(即传入数据的末尾),添加更多数据应该向前滚动视图。 但是,如果视图已向上滚动一点(即用户正在查看一些有趣的内容,例如错误消息),则应用程序应该仅在末尾添加文本而不再滚动。
我现在有两个问题:
- 我不知道如何检索当前滚动位置(调用 GetScrollPos() 似乎不起作用 - 它们只是返回 0)。
- 我不知道如何检索滚动条的当前范围(调用 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:
- I can't work out how to retrieve the current scroll position (calls to GetScrollPos() don't seem to work - they just return 0).
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,这就是我要做的:
这个简单的演示应用程序几乎可以完美运行。 除非用户单击不在文本末尾的行,否则它会整齐地滚动。 此后,它保持良好状态,以便您可以选择文本(注意:仍然存在一个错误,如果您选择向上而不是向下,它会清除您的选择)。
最大的烦恼是,如果我尝试启用“| wx.TE_RICH2”选项,一切都会变得有点梨形。 我确实需要这个来对错误进行语法突出显示,但如果我无法启用该选项,我注定只能使用单色 - 嘘!
关于如何阻止在丰富的编辑控件上滚动还有更多想法吗?
Okay, so here's where I've got to:
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?
我刚刚测试了一个简单的示例(在
wx.TextCtrl< 的
EVT_TEXT
事件处理程序中检查GetScrollPos(0)
和GetScrollRange(0)
/code>),它对我来说工作得很好 - 它们分别返回当前显示行的索引和总行数。也许问题是你的wxPython版本? 我用了:
I just tested a simple example (checking
GetScrollPos(0)
andGetScrollRange(0)
inEVT_TEXT
event handler forwx.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: