如何使用一个滚动条滚动两个平行的文本小部件?

时间:2016-03-12 12:31:50

标签: python-3.x tkinter

我需要滚动"行号"文本小部件和"代码"我正在开发的IDE中同时使用文本小部件。我怎么做到这一点?

2 个答案:

答案 0 :(得分:2)

[已编辑]在Tcl中执行此操作非常容易,因此我认为必须能够获得与以下Tcl过程相当的Tkinter:

#!/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()

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

drop

比我更了解Python的人可能会在没有列出" yview"的情况下弄清楚如何做到这一点。对于每个文本小部件单独,但这应该让你去。

答案 1 :(得分:1)

我自己的解决方案,虽然不是最好的,但是:(原谅缺乏背景)

def scroller(self,*args):#Move me
    self.text.yview(*args)
    self.numb.yview(*args)
def on_textscroll(self, *args):#Move me
    self.vsb.set(*args)
    self.scroller('moveto', args[0])