Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

python 3.x - How to scroll two parallel text widgets with one scrollbar?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

[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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...