python Tkinter-滚动两个命令



如何滚动两个命令text1text2?请帮帮我。

from tkinter import *
root = Tk()
root.title('Text editor')
root.geometry(1000x500)
text_scroll = Scrollbar(frame)
text_scroll.pack(side=RIGHT, fill=Y)
text = Text(root, width=500, height=250, font=font, fg="white", bg="gray10", selectbackground="black", selectforeground="gray", insertbackground="white", undo=True, yscrollcommand=text_scroll.set, wrap="word")
text.pack(expand="yes", fill=BOTH)
text2 = Text(root, width=500, height=250, font=font, fg="white", bg="gray10", selectbackground="black", selectforeground="gray", insertbackground="white", undo=True, wrap="word")
text2.pack(expand="yes", fill=BOTH)
root.mainloop()

您是否试图连接两个tkinter.Text以同时滚动它们?如果是这样的话,看看这个:

import tkinter as tk

def text_yview(*args):
text.yview(*args)
text2.yview(*args)
def scroll_set(*args):
text_scroll.set(*args)
text.yview("moveto", args[0])
text2.yview("moveto", args[0])

root = tk.Tk()
text_scroll = tk.Scrollbar(root)
text_scroll.pack(side="right", fill="y")
text = tk.Text(root, yscrollcommand=scroll_set)
text.pack(side="right")
text2 = tk.Text(root, yscrollcommand=scroll_set)
text2.pack(side="right")
text_scroll.config(command=text_yview)
text.insert("end", "n".join(str(i) for i in range(30)))
text2.insert("end", "n".join(str(i) for i in range(30)))
root.mainloop()

如果你想在每个文本窗口小部件的右侧添加单独的滚动条,你可以这样做:

from tkinter import *
root = Tk()
root.title('Text editor')
root.geometry('1000x500')
scroller_frame = Frame(root)
scroller_frame.pack(side=RIGHT, fill=Y)
text_scroll1 = Scrollbar(scroller_frame)
text_scroll1.pack(side=TOP, fill=Y, expand=True)
text_scroll2 = Scrollbar(scroller_frame)
text_scroll2.pack(side=TOP, fill=Y, expand=True)
text = Text(root, width=50, height=10, fg="white", bg="gray10", selectbackground="black", selectforeground="gray", insertbackground="white", undo=True, yscrollcommand=text_scroll1.set, wrap="word")
text.pack(expand="yes", fill=BOTH)
text2 = Text(root, width=50, height=10, fg="white", bg="gray10", selectbackground="black", selectforeground="gray", insertbackground="white", undo=True, wrap="word", yscrollcommand=text_scroll2.set)
text2.pack(expand="yes", fill=BOTH)
root.mainloop()

最新更新