大家好,
我最近在HTML编辑器上做过一些工作,但最终陷入了困境。
该文件本身比我的文本编辑器ProssesIT小约3.7kb,但它无法在任何平滑的地方运行。
当我打开它时,我的整个计算机开始停滞,并且在我向其中键入任何内容时都有很大的延迟。
然后,当我键入“ <”以查看其工作原理时,整个事情崩溃了,我不得不强行退出乘法。
这不是因为线程过多,因为我在google上查询了python的最大线程限制,并说最大为1021,而我一次只有5个线程。 (如果计算主循环,则为6)
这是代码:
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()
我只是想知道是否有人知道为什么会这样?
谢谢,IT