473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Tkinter Text widget getting too slow

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
Jul 18 '05 #1
7 10596
Jane Austine wrote:
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
Jul 18 '05 #2

"Jane Austine" <ja***********@hotmail.com> schrieb im Newsbeitrag
news:ba*************************@posting.google.co m...
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

Jul 18 '05 #3

"Jane Austine" <ja***********@hotmail.com> wrote in message
news:ba*************************@posting.google.co m...
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

Jul 18 '05 #4
ja***********@hotmail.com (Jane Austine) writes:
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
Jul 18 '05 #5
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
Jul 18 '05 #6

"Christos TZOTZIOY Georgiou" <tz**@sil-tec.gr> schrieb im Newsbeitrag
news:eo********************************@4ax.com...
On Thu, 4 Sep 2003 03:15:16 +0900, rumours say that "Changjune Kim"
<ju******@REMOVETHIShanmail.net> might have written:
(I'm interested to see his "extremely fast" text widget, too)


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

Jul 18 '05 #7

"Christos TZOTZIOY Georgiou" <tz**@sil-tec.gr> schrieb im Newsbeitrag
news:eo********************************@4ax.com...
On Thu, 4 Sep 2003 03:15:16 +0900, rumours say that "Changjune Kim"
<ju******@REMOVETHIShanmail.net> might have written:
(I'm interested to see his "extremely fast" text widget, too)


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_r ooty())

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.c Cols,"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,comman d=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


Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Pote | last post by:
Running my programme in Python 2.3.4 I received the following msg in the consol :- (Pent III running W2K prof) """ Exception in Tkinter callback Traceback (most recent call last): File...
1
by: Pekka Niiranen | last post by:
Hi there, after reading TkInter/thread -recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965 I wondered if it was possible to avoid using threads for the following problem: ...
2
by: Tonino | last post by:
Hi, I have a small Tkinter app that gets data from a socket connection to a "server". The app has a Text() widget to display the info that it gets from the socket connection. I have the...
8
by: Harlin Seritt | last post by:
I have the following script. Two widgets call the same function. How can I tell inside of the called function which button called it?: def say_hello(): print 'hello!' print widget root =...
8
by: Dustan | last post by:
I'm trying to get a scrollbar bound with a Frame, and I keep on getting a scrollbar, but it doesn't actually scroll. Some help, please?
4
by: Bob Greschke | last post by:
Hi! I want to grab the contents of a Text widget when the frame it's on gets destroyed. I tried TextWidget.bind("<Destroy>"... , but the widget is gone before the call gets made, and I'd really...
0
by: lee.walczak | last post by:
I actually post a topic relating to my problem here: (http://groups.google.co.uk/group/comp.lang.python/browse_thread/ thread/a073d532c4481bfe?hl=en# ) But I thought it could be useful to...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
4
by: Mudcat | last post by:
I've tried quite a few things to get this correct but have hit a couple of sticking points that I can't figure out. I need to ge the Text box to function like the 'wraplength' option in a Label. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.