Python 3.x t使用鼠标滚轮垂直滚动多个文本区域
我想创建一个带有线条条的文本区域(左侧的线条显示行号),当我用鼠标滚轮滚动时,它必须同时滚动文本区域和线条条Python 3.x t使用鼠标滚轮垂直滚动多个文本区域,python-3.x,tkinter,Python 3.x,Tkinter,我想创建一个带有线条条的文本区域(左侧的线条显示行号),当我用鼠标滚轮滚动时,它必须同时滚动文本区域和线条条 感谢您的支持我无法打开您的图片,但让我尝试帮助您 将滚动条小部件的命令选项设置为txt yview方法 scrollb = tk.Scrollbar(..., command=txt.yview) 将文本小部件的yscrollcommand选项设置为滚动条的Set方法 txt['yscrollcommand'] = scrollb.set 您需要对此进行修改,以使您的txt和滚动条
感谢您的支持我无法打开您的图片,但让我尝试帮助您 将滚动条小部件的命令选项设置为txt yview方法
scrollb = tk.Scrollbar(..., command=txt.yview)
将文本小部件的yscrollcommand选项设置为滚动条的Set方法
txt['yscrollcommand'] = scrollb.set
您需要对此进行修改,以使您的txt和滚动条对您的鼠标滚轮做出反应
要将行号添加到txt,您可以使用一个循环,每次循环时都将行号添加到下一行
下面是一个工作代码,滚动条对鼠标滚轮作出反应:
import tkinter as tk
class App(object):
def __init__(self):
self.root = tk.Tk()
# create a Frame for the Text and Scrollbar
txt_frm = tk.Frame(self.root, width=600, height=600)
txt_frm.pack(fill="both", expand=True)
# ensure a consistent GUI size
txt_frm.grid_propagate(False)
# implement stretchability
txt_frm.grid_rowconfigure(0, weight=1)
txt_frm.grid_columnconfigure(0, weight=1)
# create a Text widget
self.txt = tk.Text(txt_frm, borderwidth=3, relief="sunken")
self.txt.config(font=("consolas", 12), undo=True, wrap='word')
self.txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)
# create a Scrollbar and associate it with txt
scrollb = tk.Scrollbar(txt_frm, command=self.txt.yview)
scrollb.grid(row=0, column=1, sticky='nsew')
self.txt['yscrollcommand'] = scrollb.set
app = App()
app.root.mainloop()
请不要以图片形式发布代码,而是以文本形式将其包含在问题中。