Python tkinter使用箭头键同时滚动两个TEXT小部件 [英] Python tkinter scrolling two TEXT widgets at the same time with arrow keys

查看:40
本文介绍了Python tkinter使用箭头键同时滚动两个TEXT小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个具有 2 个文本小部件的 GUI.(我的意思是它里面有很多东西,但为了这个问题,让它留在 2 个文本小部件上).我想要做的是,当我用箭头键滚动一个文本小部件时,另一个文本小部件也会同时滚动.我能够使用滚动条(未在代码中显示)完成此操作,但不能使用箭头键.我希望同时在两个文本区域上使用箭头键的正常行为.也就是说,当它到达可查看文本的底部时,它会向下滚动,但如果我向上滚动,文本不会只移动箭头.你知道,就像任何普通的文本编辑器一样.所以问题是我如何实现这一点?这是我的代码片段.

I'm building a GUI that have 2 text widgets. (I mean it has a bunch of things in it but the sake of this question lets leave it at 2 text widgets). What I want to do is that when I scroll the one text widget with the arrow key the other text widget also scrolls at the same time. I was able to accomplish this with the scrollbar (not shown in code) but, not with the arrow keys. I want the arrow key normal behaviour on both text areas at the same time. That is to say that when it gets to the bottom of the viewable text it scrolls down but if I scroll back up the text doesn't move just the arrow. You know, like any normal text editor. So the question is how do I accomplish this? Here is a snippet of my code.

#create Text widgets

descriptionTextField = Text(mainframe, width=40, height=10)
descriptionTextField.grid(column=2, row=5, sticky=(W))
descriptionTextField.bind("<Down>", OnEntryDown)
descriptionTextField.bind("<Up>", OnEntryUp)

pnTextField = Text(mainframe, width=40, height=10)
pnTextField.grid(column=3, row=5, sticky=(W))
pnTextField.bind("<Down>", OnEntryDown)
pnTextField.bind("<Up>", OnEntryUp)

#here are what I have for code that **DOESN'T** do what I want.
def OnEntryDown(event):
    descriptionTextField.yview_scroll(1,"units")
    pnTextField.yview_scroll(1,"units")

def OnEntryUp(event):
    descriptionTextField.yview_scroll(-1,"units")
    pnTextField.yview_scroll(-1,"units")

必须有一种方法可以找出下一个箭头键何时大于可视区域(在本例中为 10),然后滚动,否则只需移动光标即可.

There has to be a way to find out when the next arrow key will be greater than the viewable area (in this case 10) and then scroll other wise just move the cursor.

注意:我无法获得向上<向上>"和向下<向下>"箭头的代码以显示在我上面的代码中,但相信我.

NOTE: I can't get the code for up "< Up >" and down "< Down >" arrow to show up in my code above but believe me it is there.

推荐答案

与其尝试复制箭头键的作用,另一种方法是在处理键后同步两个窗口(即:设置 yview一个到另一个的 yview)?如果需要,您可以同时移动插入光标.这种技术只有在两个小部件的行数相同时才有效.

Instead of trying to duplicate what the arrow key does, a different method would be to sync the two windows after the key has been processed (ie: set the yview of one to the yview of the other)? You can move the insertion cursor at the same time if you want. This technique will only work if the two widgets have the same number of lines.

虽然正确的方法是调整 bindtags 以便在类绑定之后创建绑定,但您可以使用 tkinter 处理关键 press 事件的知识来避免这种复杂化.这意味着您可以为关键的 release 事件添加绑定.不过,它会产生微小的滞后.

While the right way would be to adjust the bindtags so that you create a binding after the class bindings, you can avoid that complication with the knowledge that tkinter processes the key press events. This means you can add bindings to key release events. It yields a tiny lag though.

它看起来像这样:

descriptionTextField("<KeyRelease-Up>", OnArrow)
descriptionTextField("<KeyRelease-Down>", OnArrow)
pnTextField("<KeyRelease-Up>", OnArrow)
pnTextField("<KeyRelease-Down>", OnArrow)
...
def OnArrow(event):
    widget = event.widget
    other = pnTextField if widget == descriptionTextField else descriptionTextField
    other.yview_moveto(widget.yview()[0])
    other.mark_set("insert", widget.index("insert"))

使用绑定标签可以消除延迟.你可以这样设置:

Using bindtags eliminates the lag. You can set it up like this:

   for widget in (descriptionTextField, pnTextField):
        bindtags = list(widget.bindtags())
        bindtags.insert(2, "custom")
        widget.bindtags(tuple(bindtags))

        widget.bind_class("custom", "<Up>", OnArrow)
        widget.bind_class("custom", "<Down>", OnArrow)

这篇关于Python tkinter使用箭头键同时滚动两个TEXT小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆