Tkinter Text widget getting too slow

J

Jane Austine

As you add more items, say text lines, in Text widget, it gets too
slow and almost impractical to use on. Take idle for example. If the
text gets bigger(e.g. print
urllib.urlopen('http://www.amazon.com').read() ), it becomes too
sluggish to use as an "interactive" shell.

I have tried wxPython and it seems to have the same problem (from my
experience using PyCrust).

Is there any way to speed up Text widget, or should I look for another
GUI library?

Jane
 
P

Peter Otten

Jane said:
As you add more items, say text lines, in Text widget, it gets too
slow and almost impractical to use on. Take idle for example. If the
text gets bigger(e.g. print
urllib.urlopen('http://www.amazon.com').read() ), it becomes too
sluggish to use as an "interactive" shell.

I have tried wxPython and it seems to have the same problem (from my
experience using PyCrust).

Is there any way to speed up Text widget, or should I look for another
GUI library?

Jane

Does the same problem appear if you read a file of the same size from your
harddisk or is it just the web site that does not respond fast enough?

In this case you could put urlopen() into a separate thread.

Peter
 
M

Michael Peuser

Jane Austine said:
As you add more items, say text lines, in Text widget, it gets too
slow and almost impractical to use on. Take idle for example. If the
text gets bigger(e.g. print
urllib.urlopen('http://www.amazon.com').read() ), it becomes too
sluggish to use as an "interactive" shell.

I have tried wxPython and it seems to have the same problem (from my
experience using PyCrust).

Is there any way to speed up Text widget, or should I look for another
GUI library?

This seems to be a problem with most naivly written wigdets which store
their data in some internal format. What I have done several times is
writing my own widgets. This is possible for Tkinter by using Fredrik
Lundh's WCK (Widget Construction Kit)
http://effbot.org/zone/wck.htm
He has some tutorials and the whole thing is well integrated in Tkinter. He
especially has a tutorial on using HUGE datasets.

Of course you have to undergo some programming for all the features you
would want to use in your text widget. (Highliting, linebreaks,..etc)

I implemented a binary editor some time ago, which is extremely fast with
even a 100 MB file. (For I/O it used memory mapped files, but this is not
the reason it is fast; it just simplified the programmin :))

Kindly
Michael P
 
C

Changjune Kim

Jane Austine said:
As you add more items, say text lines, in Text widget, it gets too
slow and almost impractical to use on. Take idle for example. If the
text gets bigger(e.g. print
urllib.urlopen('http://www.amazon.com').read() ), it becomes too
sluggish to use as an "interactive" shell.

I have tried wxPython and it seems to have the same problem (from my
experience using PyCrust).

Is there any way to speed up Text widget, or should I look for another
GUI library?

Jane

Yes, it gets real slow. It happens quite often when a long single line is
shown on the window currently, like in your amazon html example.

Try this instead,

pprint(urllib.urlopen('http://www.amazon.com').readlines())

It won't get slow even when it's still on the screen; it inserts multiple
(short) lines. If you have to insert a lot of long lines, maybe you have to
look into WCK and customize it as Michael said in his posting to the
thread. (I'm interested to see his "extremely fast" text widget, too)

Best regards,

June Kim
 
J

John J. Lee

As you add more items, say text lines, in Text widget, it gets too
slow and almost impractical to use on. Take idle for example. If the
text gets bigger(e.g. print [...]

Is there any way to speed up Text widget, or should I look for another
GUI library?

I'm surprised that you're having trouble with it, actually, but:

How about something like Scintilla? I'm not sure about wx and Tk, but
Scintilla is available for GTk and Qt, IIRC (the latter is a port,
called QScintilla). There might well be something similar for wx or
Tk. Or maybe the standard Qt or GTk text widgets are themselves more
scalable than their wx or Tk counterparts -- no idea.

If you specifically want a Python shell, I think eric3 uses
QScintilla, so there should be code in there you can use.


John
 
N

Neil Hodgson

John J. Lee:
How about something like Scintilla?

The original poster had tried PyCrust which uses the wxWindows port of
Scintilla wxStyledTextCtrl. The design centre for Scintilla is editing
source code files and its assumptions are incorrect for very large documents
or very wide documents. The performance of Scintilla is dependent on how it
is used as much as its design and it can be sped up greatly by turning off
styling and word wrap.

Neil
 
M

Michael Peuser

Christos TZOTZIOY Georgiou said:
Notice that Michael Peuser described an "extremely fast" "binary
editor"; I assume a dual view (hex / chars) one. This is not a text
widget --you don't have to account for line feeds and variable width
characters :)

Exactly! Most of the time of general text edit or table widgets stays in
.font.measure()
You will get an immediate speed up if you use fixed size fonts and just use
the string length for computations. I am looking for a postable short
version of my editor (I remember I started with something less 100 lines. It
grew of course ;-) Maybe I even should install a nice homepage - if I had
the time...

Kindly
Michael P
 
M

Michael Peuser

Christos TZOTZIOY Georgiou said:
Notice that Michael Peuser described an "extremely fast" "binary
editor"; I assume a dual view (hex / chars) one. This is not a text
widget --you don't have to account for line feeds and variable width
characters :)

Well I have somthing a little bit over 100 lines; I removed the editor part
but that is straight forward.
No scaling to size up to 1 GB.
-----------------------------------
# wckhwex by Michael Peuser May-2003
# Using some ideas from F. Lundh (WCK)
# This is Open Source. I will be happy if it could help somebody.

versionString="wckhex 0.3 by mpeuser.de"
from __future__ import division

from Tkinter import *
from WCK import Widget, EventMixin
from tkFileDialog import askopenfile
import mmap

class HexView(EventMixin, Widget):
"""Displays HUGE binary file using memory mapped files
"""

aCols=10 # address field
bCols=2.5*16+1 # binary field
cCols=16+1 # ascii field

ui_takefocus=1
ui_doublebuffer=0

def __init__(self,root, buffer=None):
self.buffer=buffer
self.defLines=(len(buffer)+15)//16
self.firstLine=0 # numer of first line in window
self.oldFirst=-1 # .. compared to last time
self.visLines=1 # visible lines in window
self.pix=None # pixmap will be created on resize

self.ui_init(root)
self.vscroll=Scrolls(self,orient=VERTICAL)
self.vscroll.pack(side=RIGHT,fill=Y)

self.pack(side=TOP,fill=BOTH,expand=1)
self.font=self.ui_font(0,"Courier 10")
(self.cWidth, self.lHeight) = self.font.measure("X")

def ui_handle_resize(self, width, height):

self.height=height
self.width= max(width,
(self.aCols+1+self.bCols+1+self.cCols)*self.cWidth) #
pixel
self.visLines=self.height//self.lHeight
self.pix=self.ui_pixmap(self.width, self.height)
self.vscroll.doScroll() # compute new thumb
self.drawPix()

def ui_handle_clear(*whatever):
pass # no need for clear as is pixmap long enough

def ui_handle_repair(self, draw, x0, y0, x1, y1):
#if self.pix:
draw.paste(self.pix)

def onclick(self, ev):
self.focus_set()
if ev.num==1:
pass #XXX implement editor here
if ev.num==3:
pop=Menu(self,tearoff=0);
pop.add_command(label=versionString,state=DISABLED)
pop.post(ev.x+self.winfo_rootx(),ev.y+self.winfo_rooty())

def onkey(self, event):
todo ={'Down': ("scroll",1,"lines"), 'Up': ('scroll',-1,'lines'),
'Next': ("scroll",1,"pages"), 'Prior':('scroll',-1,'pages')}
x=todo.get(event.keysym)
print x
if x: self.vscroll.doScroll(*x)

def bgText(self,x,y,w,h,text,theBrush):
"Displays centered text with background color"
self.pix.rectangle((x+1,y+1,x+w-2,y+h-2),theBrush)
(tw,th)=self.pix.textsize(text,self.font)
self.pix.text((x+(w-tw)/2,y+(h-th)/2),text,self.font)

def drawPix(self,unchangedSize=0):
"Central dawing routine"
c=self.pix
repair=None
whiteBrush=self.ui_brush(0xffffff)
yellowBrush=self.ui_brush(0xffff99)
grayPen=self.ui_pen(0x999999,2)
ww,wh = self.ui_size()
h=self.lHeight
posX = 0

# speed up scrolling by image blitting, but unclean parametrisation of paste
if unchangedSize: ## check if blitting
useful
linesTBS = self.oldFirst-self.firstLine ## TBS = to be scrolled
if 0 < linesTBS < self.visLines/2: ## down, repair top
c.paste(c,(0, h*linesTBS))
theRange=range(1,linesTBS+1)
c.rectangle((0,h,ww,(linesTBS+1)*h),whiteBrush)
c.rectangle((0,h,self.aCols*self.cWidth,(linesTBS+1)*h),
yellowBrush)
elif 0 < -linesTBS < self.visLines/2: ## up, repair bottom
c.paste(c,(0, h*linesTBS))
delta=self.visLines+linesTBS
theRange=range(delta-1,self.visLines)
c.rectangle((0, delta*h, ww, wh),whiteBrush)
c.rectangle((0,delta*h, self.aCols*self.cWidth, wh),
yellowBrush)
else:
unchangedSize=0 # blitting would not make much
improvement...
self.oldFirst=self.firstLine # remember first line for
next time
# end of blitting optimization

if not unchangedSize: # all new
c.rectangle((0,0,ww,wh),whiteBrush)
theRange=range(1, self.visLines)
c.rectangle((0,h,self.aCols*self.cWidth,wh), yellowBrush)

for w, text in \

((self.aCols,"address"),(self.bCols,"hex"),(self.cCols,"ascii")):
w *= self.cWidth
if repair!='T':
self.bgText(posX, 0, w, h, text, yellowBrush)
c.line((posX - 1, 0, posX - 1, wh -1), grayPen)
posX+=w

# addresses and data
posY = self.lHeight
for y in theRange:
posX = 0
heightY = self.lHeight
address=(y-1+self.firstLine)*16
vals = self.buffer[address:address+16]
c.text((posX,y*self.lHeight),
"%3x.%05x " % divmod(address, 1024*1024), self.font)
posX += self.aCols*self.cWidth

for vala in range(address,address+16):
if vala %8==0: posX+=self.cWidth//2 # some in between
space
if vala<len(buffer):
c.text((posX,
y*heightY),"%02x"%ord(buffer[vala]),self.font)
posX += 2.5*self.cWidth

c.text((posX+10,y*heightY),buffer[address:address+16],self.font)
self.ui_damage()
self.focus_set()

class Scrolls(Scrollbar):
"wrapper around Tk scrolbars"
def __init__(self,frame,orient=None,**kw):
Scrollbar.__init__(self,frame,orient=orient,command=self.doScroll)
self.visible=1 # start packed
self.theMaster=frame

def doScroll(self,a=None,b=None,c=None):
m=self.theMaster
oldfirst=m.firstLine
if a=="scroll":
if c=='pages': b=int(b)*m.visLines-1
m.firstLine = oldfirst+int(b)
elif a=="moveto":
m.firstLine=int(float(b)*(m.defLines+1))
if m.firstLine+m.visLines>m.defLines: # the end
m.firstLine=m.defLines-m.visLines+1
if m.firstLine<0:
m.firstLine=0
size = m.visLines/m.defLines # 0...1
start= m.firstLine/m.defLines # 0...1
if self.visible: self.set(start,start+size)
if oldfirst != m.firstLine: m.drawPix(unchangedSize=1)

if __name__ == "__main__":
root=Tk()
root.iconify()
fbb=askopenfile("r+"); assert fbb, "No file"
buffer=mmap.mmap(fbb.fileno(),0)

s=HexView(root, buffer=buffer)

root.title("%s (%6.3f MB)" %(fbb.name, len(buffer)/1024/1024))
root.geometry("720x550+10+10")
root.deiconify()
root.mainloop()
fbb.close()
# the End
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top