gpt4 book ai didi

python - 如何在显示大量文本时加快滚动响应速度

转载 作者:太空狗 更新时间:2023-10-30 00:19:08 27 4
gpt4 key购买 nike

我正在尝试创建一个 Python 脚本来突出显示 .txt 文件中的特定模式。为此,我修改了一个脚本,该脚本使用 Tkinter 突出显示一组给定的数据。但是,我倾向于让它处理的文件大约有 10000 行,这导致滚动缓慢,因为我认为它呈现所有内容 - 无论它是否在屏幕上(如果我是,请纠正我错误的)。是否可以更改我的代码以更有效地呈现输出?我曾尝试寻找一种方法来做到这一点,但我自己没有找到任何东西。

我的代码如下:

from Tkinter import *

class FullScreenApp(object):
def __init__(self, master, **kwargs):
self.master=master
pad=3
self._geom='200x200+0+0'
master.geometry("{0}x{1}+0+0".format(
master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
master.bind('<Escape>',self.toggle_geom)
def toggle_geom(self,event):
geom=self.master.winfo_geometry()
print(geom,self._geom)
self.master.geometry(self._geom)
self._geom=geom

root = Tk()
app = FullScreenApp(root)
t = Text(root)
t.pack()

#Import file
with open('data.txt') as f:
for line in f:
t.insert(END, line)

#Search terms - Leave blank if not required
search_term0 = '0xCAFE'
search_term1 = '0x0011'
search_term2 = '0x961E'
search_term3 = '0x0000'
search_term4 = ''

#Assigns highlighted colours for terms not blank
t.tag_config(search_term0, background='red')
if search_term1 != '':
t.tag_config(search_term1, background='red')
if search_term2 != '':
t.tag_config(search_term2, background='red')
if search_term3 != '':
t.tag_config(search_term3, background='red')
if search_term4 != '':
t.tag_config(search_term4, background='red')

#Define search
#Requires text widget, the keyword, and a tag
def search(text_widget, keyword, tag):
pos = '1.0'
while True:
idx = text_widget.search(keyword, pos, END)
if not idx:
break
pos = '{}+{}c'.format(idx, len(keyword))
text_widget.tag_add(tag, idx, pos)

#Search for terms that are not blank
search(t, search_term0, search_term0)
if search_term1 != '':
search(t, search_term1, search_term1)
if search_term2 != '':
search(t, search_term2, search_term2)
if search_term3 != '':
search(t, search_term3, search_term3)
if search_term4 != '':
search(t, search_term4, search_term3)

root.mainloop()

文件中的数据示例在以下链接中给出:here

非常感谢您的宝贵时间,非常感谢。

最佳答案

假设 MCVE 如下:

import tkinter as tk


def create_text(text_len):
_text = list()
for _ in range(text_len):
_text.append("{}\n".format(_))
_text = "".join(_text)
return _text


if __name__ == '__main__':
root = tk.Tk()
txt = tk.Text(root)

txt.text = create_text(10000)
txt.insert('end', txt.text)

txt.pack()
root.mainloop()

分析

基于 this我不认为这是渲染问题。我认为 fixed rate of registering <KeyPress> events 是个问题.这意味着每秒注册的事件数是固定的,即使硬件可能能够以更快的速度注册。类似的规定也适用于鼠标滚动事件。


渲染解决方案

可能为 txt['height'] 的缓冲区比例切片文本有助于。但无论如何,这不应该是 Tk 应该呈现的方式吗?


渲染无关问题的解决方案

如果将一个步骤定义为光标移动到上一行或下一行,对于每个已注册的UpDown 事件;然后scrolling_speed = step * event_register_frequency .


通过增加步长

一个简单的解决方法是简单地增加步长,就像增加要跳转的行数一样,为每个键绑定(bind)注册。

但是已经有这样的默认行为,假设页面长度 > 1 行,Page UpPage Down 的步长为一页。这使得滚动速度增加,即使事件注册率保持不变。

或者,可以定义一个具有更大步长的新事件处理程序来为每次注册UpDown调用多个光标移动,例如:

import tkinter as tk


def create_text(text_len):
_text = list()
for _ in range(text_len):
_text.append("{}\n".format(_))
_text = "".join(_text)
return _text


def step(event):
if txt._step_size != 1:
_no_of_lines_to_jump = txt._step_size
if event.keysym == 'Up':
_no_of_lines_to_jump *= -1

_position = root.tk.call('tk::TextUpDownLine', txt, _no_of_lines_to_jump)
root.tk.call('tk::TextSetCursor', txt, _position)
return "break"

if __name__ == '__main__':
root = tk.Tk()
txt = tk.Text(root)

txt.text = create_text(10000)
txt.insert('end', txt.text)

txt._step_size = 12
txt.bind("<Up>", step)
txt.bind("<Down>", step)

txt.pack()
root.mainloop()

通过模仿按键事件注册率增加:

here 中所述实际上修改按键注册表率超出了 Tk 的范围。相反,它可以被模仿:

import tkinter as tk


def create_text(text_len):
_text = list()
for _ in range(text_len):
_text.append("{}\n".format(_))
_text = "".join(_text)
return _text


def step_up(*event):
_position = root.tk.call('tk::TextUpDownLine', txt, -1)
root.tk.call('tk::TextSetCursor', txt, _position)

if txt._repeat_on:
root.after(txt._repeat_freq, step_up)
return "break"


def step_down(*event):
_position = root.tk.call('tk::TextUpDownLine', txt, 1)
root.tk.call('tk::TextSetCursor', txt, _position)

if txt._repeat_on:
root.after(txt._repeat_freq, step_down)
return "break"


def stop(*event):
if txt._repeat_on:
txt._repeat_on = False
root.after(txt._repeat_freq + 1, stop)
else:
txt._repeat_on = True

if __name__ == '__main__':

root = tk.Tk()

txt = tk.Text(root)

txt.text = create_text(10000)
txt.insert('end', txt.text)
txt._repeat_freq = 100
txt._repeat_on = True

txt.bind("<KeyPress-Up>", step_up)
txt.bind("<KeyRelease-Up>", stop)
txt.bind("<KeyPress-Down>", step_down)
txt.bind("<KeyRelease-Down>", stop)

txt.pack()
root.mainloop()

通过增加步长和模仿注册率增加

import tkinter as tk


def create_text(text_len):
_text = list()
for _ in range(text_len):
_text.append("{}\n".format(_))
_text = "".join(_text)
return _text


def step_up(*event):
_no_of_lines_to_jump = -txt._step_size
_position = root.tk.call('tk::TextUpDownLine', txt, _no_of_lines_to_jump)
root.tk.call('tk::TextSetCursor', txt, _position)

if txt._repeat_on:
root.after(txt._repeat_freq, step_up)
return "break"


def step_down(*event):
_no_of_lines_to_jump = txt._step_size
_position = root.tk.call('tk::TextUpDownLine', txt, _no_of_lines_to_jump)
root.tk.call('tk::TextSetCursor', txt, _position)

if txt._repeat_on:
root.after(txt._repeat_freq, step_down)
return "break"


def stop(*event):
if txt._repeat_on:
txt._repeat_on = False
root.after(txt._repeat_freq + 1, stop)
else:
txt._repeat_on = True

if __name__ == '__main__':

root = tk.Tk()

txt = tk.Text(root)

txt.text = create_text(10000)
txt.insert('end', txt.text)
txt._step_size = 1
txt._repeat_freq = 100
txt._repeat_on = True

txt.bind("<KeyPress-Up>", step_up)
txt.bind("<KeyRelease-Up>", stop)
txt.bind("<KeyPress-Down>", step_down)
txt.bind("<KeyRelease-Down>", stop)

txt.pack()
root.mainloop()

关于python - 如何在显示大量文本时加快滚动响应速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38752923/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com