在Python中显示大量格式化文本
我有两个相同大小的大文件。一种是 ASCII 纯文本,另一种是颜色编码的覆盖层,对应文件中每个文本字符一个字节。
这些文件可能很大 - 最大 2.5 MB;可能会更多,之后可能会超过 100MB。
我想在可滚动文本查看器中显示文本,使用第二个文件作为叠加层。看起来像这样:
Tkinter 文本窗口,带有颜色标记文本 http://img713.imageshack.us/img713/2584 /statsviewer01d.png
所以我用Python制作了一个简单的tkinter文本小部件窗口,带有滚动条等。
我的代码如下所示:
hottest = 0
for heat in heatmap:
hottest = max(hottest,ord(heat))
hottest += 1
for heat in xrange(0,hottest):
factor = int((float(heat)/float(hottest))*100.0)
# an observation; tkinter seems to normalise custom colours
# to nearest in it's palette, which means I can't use custom
# gradients of red; if anyone knows how to use subtle custom colours?
bgcolour = "gray%d" % (100-factor)
fgcolour = "gray%d" % factor
text.tag_config("n%d"%heat,background=bgcolour,foreground=fgcolour)
text.insert("1.0",f.read())
ofs = 0
for heat in heatmap:
if 0 != ord(heat):
coord_start = "1.0 + %d chars"%ofs
coord_stop = "1.0 + %d chars"%(ofs+1)
text.tag_add("n%d"%ord(heat),coord_start,coord_stop)
ofs += 1
text.config(state=DISABLED)
text.focus()
但是,我遇到了可怕的性能问题:
加载文本
- 如果我只是使用textwidget.text = textfile.read(),它会立即加载,即使对于大文件也是如此;但问题是用颜色编码它。 (参见上面的代码)。为了添加标签,一次一个字符,我似乎强制它采用画家算法;当文件大小约为 300KB 时,我放弃等待它加载
- 如果我从文本文件和颜色文件中读取下一个字符,并附加正确标记的字符,则需要很长时间;文件大于 200KB,我放弃等待它加载
滚动。如果当前窗口中可见这么多格式,则
如果我在 Dephi 或 wxWidgets 等中解决这个问题,我将有一个自定义绘制的控件。
Python 和 Tkinter 中最直接的方法是什么?
I have two large identical-sized files. One is ASCII plain text, and the other is a colour-coded overlay, one byte per text character in the corresponding file.
These files can be large - upto 2.5 MB; possibly substantially more, perhaps over 100MB later.
I want to display the text is a scrollable text viewer, using the second file as the overlay. Looking something like this:
So I made a simple tkinter Text widget window in Python with scrollbar and such.
My code looks like this:
hottest = 0
for heat in heatmap:
hottest = max(hottest,ord(heat))
hottest += 1
for heat in xrange(0,hottest):
factor = int((float(heat)/float(hottest))*100.0)
# an observation; tkinter seems to normalise custom colours
# to nearest in it's palette, which means I can't use custom
# gradients of red; if anyone knows how to use subtle custom colours?
bgcolour = "gray%d" % (100-factor)
fgcolour = "gray%d" % factor
text.tag_config("n%d"%heat,background=bgcolour,foreground=fgcolour)
text.insert("1.0",f.read())
ofs = 0
for heat in heatmap:
if 0 != ord(heat):
coord_start = "1.0 + %d chars"%ofs
coord_stop = "1.0 + %d chars"%(ofs+1)
text.tag_add("n%d"%ord(heat),coord_start,coord_stop)
ofs += 1
text.config(state=DISABLED)
text.focus()
However, I run into horrid performance problems:
loading the text
- if I simply use textwidget.text = textfile.read() it loads instantly, even for the large file; but then the problem is colour-coding it. (see code above). To add the tags, one character at a time, it seems I force it to take a painter algorithm; by the time the file is about 300KB in size, I give up waiting for it to load
- if I read the next character from the textfile and the colourfile, and append a properly tagged character, it takes forever; a file bigger than about 200KB and I give up waiting for it to load
Scrolling. If this much formatting visible in the current window, it goes very slowly
If I was approaching this problem in, say, Dephi or wxWidgets or whatever, I'd have a custom-drawn control.
What's the most straightforward way in Python and Tkinter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何使用可滚动画布,并且只绘制用户公开的文本/热图?无论文件大小如何,这都可以让您快速进行初始绘制,并在物体移动时快速重绘。
如果您想要更快的速度和更多的控制,那么您将需要某种虚拟画布,其中只有显示的区域及其周围的区域实际存在,其他任何内容仅在被引用时才绘制。我认为 TkInter 并没有给你那么多的控制权,尽管像 Widget Construction Kit (WCK) 这样的东西应该可以做到。
How about using a scrollable canvas instead, and only ever drawing the text/heatmap that is exposed by the user? That should give you a quick initial draw and a quick redraw when things move around, regardless of the size of the file.
If you want more speed and more control, then you would need some sort of virtual canvas where only the area on display and an area around it actually exists, anything else is only drawn as and when it gets referenced. I don't think TkInter gives you that much control, although things like the Widget Construction Kit (WCK) should do.