如何使用一个滚动条滚动两个平行文本小部件? [英] How to scroll two parallel text widgets with one scrollbar?

查看:43
本文介绍了如何使用一个滚动条滚动两个平行文本小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我正在开发的 IDE 中同时滚动行号"文本小部件和代码"文本小部件.我将如何实现这一目标?

解决方案

在 Tcl 中做到这一点真的很容易,所以我认为必须有可能获得与以下 Tcl 过程等效的 Tkinter:

>

proc rollon {boxes args} {foreach 框 $boxes {评估 {$box yview} $args}}

经过几次失败的努力,我想出了这个,它有效:

#!/usr/bin/env python3从 tkinter 导入 *从 tkinter 导入 ttk根 = Tk()def viewall(*args):全球交易,交易 2eval('tx.yview(*args)')eval('tx2.yview(*args)')tx = 文本(根,背景 = '白色',宽度 = '20',高度 = '8')tx2 = 文本(根,背景 = '白色',宽度 = '20',高度 = '8')rolly = ttk.Scrollbar(root, orient=VERTICAL, command=viewall)tx['yscrollcommand'] = rolly.settx2['yscrollcommand'] = rolly.settx.grid(row=0, column=0,sticky=(N, W, E, S))tx2.grid(row=0, column=1,sticky=(N, W, E, S))rolly.grid(row=0, column=2,sticky=(N, W, E, S))root.mainloop()

比我更了解 Python 的人可能会想出如何做到这一点,而无需分别列出每个文本小部件的yview",但这应该可以帮助您.

I need to scroll a "line number" text widget and a "code" text widget simultaneously in an IDE I'm developing. How would I achieve this?

解决方案

[EDITED] It's really easy to do this in Tcl, so I figured it had to be possible to get a Tkinter equivalent of the following Tcl procedure:

proc rollon {boxes args} {
    foreach box $boxes {
        eval {$box yview} $args
     }
}

After a few failed efforts, I came up with this, which works:

#!/usr/bin/env python3
from tkinter import *
from tkinter import ttk
root = Tk()
def viewall(*args):
    global tx, tx2
    eval('tx.yview(*args)')
    eval('tx2.yview(*args)')
tx = Text(root, background='white', width = '20', height = '8')
tx2 = Text(root, background='white', width = '20', height = '8')
rolly = ttk.Scrollbar(root, orient=VERTICAL, command=viewall)
tx['yscrollcommand'] = rolly.set
tx2['yscrollcommand'] = rolly.set
tx.grid(row=0, column=0, sticky=(N, W, E, S))
tx2.grid(row=0, column=1, sticky=(N, W, E, S))
rolly.grid(row=0, column=2, sticky=(N, W, E, S))
root.mainloop()

Somebody who knows more Python than I do could probably figure out how to do this without listing the "yview" for each text widget separately, but this should get you going.

这篇关于如何使用一个滚动条滚动两个平行文本小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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