Hello all,
I have been doing some work on my HTML editor lately, and I have come to a (what seams like a) dead end.
The file its self is about 3.7kb smaller than my text editor, ProssesIT, and yet it fails to run any where near as smoothly.
When I open it up, my whole computer starts to lag, and there is a massive delay in when i type anything into it.
Then, when I type in "<" to see if its working, the whole thing crashes and I have to force quit the aplication.
This is not a result of having to many threads, because i looked up the python maximum thread limit on google, and it said that the maximum is 1021, while I only have 5 going at one time. (6 if you count the mainloop)
here is the code:

from Tkinter import *
import threading
import webbrowser
import random
import tkColorChooser as tcc
import tkFileDialog as tfd
import tkMessageBox as tmb

root = Tk()
root.title("htmIT")
text = Text(root, font=('Liberation Serif', 14))

casevar = IntVar()

class Find(threading.Thread): #thanks to www.java2s.com (HEAVALY MODIFIED)
    def run(self):
        variables.entryText = edit.get()
        while True:
            if edit.get() != variables.entryText:
                text.tag_remove('found', '1.0', END)
                variables.entryText = edit.get()
                if variables.entryText:
                    idx = '1.0'
                    while True:
                        try:
                            idx = text.search(variables.entryText, idx, nocase=casevar.get(), stopindex=SEL_LAST)
                        except TclError:
                            idx = text.search(variables.entryText, idx, nocase=casevar.get(), stopindex=END)
                        if not idx: break
                        lastidx = '%s+%dc' % (idx, len(variables.entryText)) #
                        text.tag_add('found', idx, lastidx)     #
                        idx = lastidx                           #
                    text.tag_config('found', background='#0000F0') #

class TagApply(threading.Thread):
    def run(self):
        while True:
            try:
                genTag()
                text.tag_add(variables.tagList[variables.tagNum], variables.idx2, variables.lastidx2)
                text.tag_config(variables.tagList[variables.tagNum], background='blue')
                variables.tagNum += 1
            except AttributeError:
                pass

class AutoSave(threading.Thread):
    def run(slef):
        while True:
            try:
                variables.autoSave = text.get(1.0, END)
            except:
                break

class TagTo1(threading.Thread):
    def run(self):
        variables.tag1 = '<'
        while True:
            genTag()
            if variables.tag1:
                variables.idx2 = '1.0'
                while True:
                    variables.idx2 = text.search(variables.tag1, variables.idx2, stopindex=END)
                    if not variables.idx2: break
                    variables.lastidx2 = '%s+%dc' % (variables.idx2, len(variables.idx2))
                    text.tag_add(variables.tagList[variables.tagNum], variables.idx2, variables.lastidx2)    
                    variables.idx2 = variables.lastidx2
                text.tag_config(variables.tagList[variables.tagNum], background='blue')
                variables.tagNum += 1

class TagFrom1(threading.Thread):                                                                          
    def run(self):                                                                                        
        variables.tag2 = '>'                                                                              
        while True:                                                                                        
            genTag()                                                                                      
            if variables.tag2:                                                                            
                variables.idx = '1.0'                                                                      
                while True:                                                                                
                    variables.idx = text.search(variables.tag2, variables.idx, stopindex=END)              
                    if not variables.idx: break                                                            
                    variables.lastidx = '%s+%dc' % (variables.idx, len(variables.idx))                    
                    text.tag_add(variables.tagList[variables.tagNum], variables.idx, variables.lastidx)    
                    variables.idx = variables.lastidx                                                      
                text.tag_config(variables.tagList[variables.tagNum], background='blue')                    
                variables.tagNum += 1                                                                      

class Variables:
    pass
variables = Variables()
variables.autoSave = ''
variables.tagList  = []
variables.tagNum   = 0

def getNums():
    h = text.get(1.0, END).split('\n')
    #blue
    for row in range(1, len(h)):
        print 'HHHHH'
        for column in range(1, len(h[row])):
            if text.get('%s.%s' %(row, column-1), '%s.%s' %(row, column)) == '<':
                print 1
                variables.startPoint = '%s.%s' %(row, column)
            elif text.get('%s.%s' %(row, column-1), '%s.%s' %(row, column)) == '>':
                variables.endPoint = '%s.%s' %(row, column)
                print 2
            else:
                print 3
                variables.endPoint   = 0.0
                variables.startPoint = 0.0
                

def new():
    if tmb.askyesno('Are you sure?', 'All unsaved work will be lost'):
        text.delete(1.0, END)

def saveAsFile():
    try:
        tfd.asksaveasfile().write(text.get(1.0, END))
    except AttributeError:
        pass
    variables.saved = text.get(1.0, END)
    
def openFile():
    if tmb.askyesno('Are you sure?', 'All unsaved work will be lost'):
        text.delete(1.0, END)
        text.insert(END, tfd.askopenfile().read())

def afterQuit():
    if variables.saved != variables.autoSave:
        if not tmb.askyesno('You havent saved', 'Quit without saving?'):
            tfd.asksaveasfile().write(variables.autoSave)

def genTag():
    ns = ''
    for x in range(6): #digits
        ns += random.choice('a b c d e f g h i f k l m n o p q r s t u v w x y z'.split())
    variables.tagList.append(ns)
    
def pexit():
    if tmb.askyesno('Save?', 'Would you like to save before you exit?'):
        saveAsFile()
    root.destroy()
    
def showContact():
    contact = Tk()
    contact.title('Contact me')
    Label(contact, text=' ').grid()
    Label(contact, text='Please E-Mail me any feedback, or question you').grid()
    Label(contact, text='may have, at this E-mail adress:').grid()
    mail = Entry(contact, width=21)
    mail.insert(END, 'itai_likes_pie@hotmail.com')
    mail.grid()
    Button(contact, text='Close', command=contact.destroy).grid()
    Label (contact, text=' ').grid()

menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)

menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=new)
filemenu.add_command(label="Open", command=openFile)
filemenu.add_command(label="Save As", command=saveAsFile)
filemenu.add_separator()
filemenu.add_command(label='Undo', command=text.edit_undo)
filemenu.add_command(label='Redo', command=text.edit_redo)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=pexit)

Label(root, text='Text to find:').grid(columnspan=2)
edit = Entry(root, bg='white', width=30)
edit.grid(row=0, column=1, columnspan=2)
case = Checkbutton(root, text='Case sensative off', variable=casevar)
case.grid(row=0, column=2, columnspan=2)
scrl = Scrollbar(root, command=text.yview, width=30)
text.config(yscrollcommand=scrl.set, bg='white', maxundo=100)
text.grid(columnspan=3)
scrl.grid(row=1, column=3, columnspan=1, sticky='ns')

TagFrom1().start()
TagTo1().start()
TagApply().start()
AutoSave().start()
Find().start()
root.mainloop()
pexit()

I was just wandering if any one knows why this is happening?
Thanks, The-IT

Recommended Answers

All 7 Replies

bumping coz i need help badly

I don't have time to test extensively your code but i'd begin by looking at loops.
I've seen a couple of while True...
How long do you stay in these loops ?
You should test and locate precisely where your program lose time.

I see you are using deamon threading, but be advised that the standard CPython uses GIL (Global Interpreter Lock) allowing only one thread of code to execute at any given time. That could slow things down depending what's in the endless loop. Try/except pairs are notoriously slow.

Python3 (also Python26) offers module multiprocessing that takes care of this by side-stepping the Global Interpreter Lock, using subprocesses instead of threads.

thanks a lot man,
there is just one problem, my version of ubuntu does not support any version of python higher than 2.5. awww well. at least i know now to stop trying to get this one to work and start a new project witch might work.

I don't have time to test extensively your code but i'd begin by looking at loops.
I've seen a couple of while True...
How long do you stay in these loops ?
You should test and locate precisely where your program lose time.

they must continue while the root window is open.

Maybe you're right... I didn't look at that very much but you have some

while True:
    ...
    while True:
        ...

That i don't like very much... But in this case, i maybe wrong.
Anyway, vegaseat seems to know about threading and I don't so you'd better listen to him.

thanks a lot man,
there is just one problem, my version of ubuntu does not support any version of python higher than 2.5. awww well. at least i know now to stop trying to get this one to work and start a new project witch might work.

That's not entirely true. You can use the Synaptec Package Manager to download the latest versions of Python. FYI.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.