Tkinter ScrolledText Widget滚动非常慢

时间:2019-09-09 00:53:32

标签: python tkinter

即使使用os正在加载67 kb的文本,滚动速度也非常慢。如何使滚动更流畅?这是我在ScrolledText小部件中设置文本的方式:

import Tkinter as tk
import ScrolledText as tkst
import os

master = tk.Tk()

read_only = tkst.ScrolledText(master, width=20, height=10, wrap='word')
read_only.bind("<Key>", lambda e: "break")
read_only.pack()

text = open(directory + '/' + file).read()
read_only.delete('1.0', tk.END)
read_only.insert(tk.INSERT, text)

我也尝试将普通的Text小部件与Scrollbar一起使用,但是我遇到了同样的问题:

from Tkinter import *

root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)

read_only = tk.Text(root, width=20, height=10, wrap='word', yscrollcommand=scrollbar.set)
read_only.bind("<Key>", lambda e: "break")

text = open(directory + '/' + file, "r").read()
read_only.delete('1.0', tk.END)
read_only.insert(tk.INSERT, text)

read_only.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=read_only.yview)

mainloop()

1 个答案:

答案 0 :(得分:0)

我放弃了Tkinter,而是选择了wxPython。这是使用wx的可滚动文本窗口的实现,即使有很多文本也可以平滑滚动:

import wx

text = 'Your Text Here'

class ScrollableTextWindow(wx.Frame):

    def __init__(self, parent):
        self.textpanel = wx.TextCtrl(parent, style=wx.TE_MULTILINE | wx.TE_READONLY)
        self.textpanel.SetValue(text)


app = wx.App(0)
frame = wx.Frame(None, wx.ID_ANY)
fa = ScrollableTextWindow(frame)
frame.Show()
app.MainLoop()

感谢泰克的树屋的答案:http://teck78.blogspot.com/2017/04/a-simple-solution-to-scrolling-text.html