我正在使用tkinter,我試圖將一個字符串[在這種情況下,原始文件]與文本小部件中的文本進行比較。這裏的功能,並在那裏我調用該函數:調用RuntimeError中的結果後:超出最大遞歸深度
def checkText():
if(text.get("1.0", "end-1c") != currentText):
print("Changed")
root.after(1000, checkText())
,並在那裏我呼籲:
root.after(1000, checkText()) #after 1 second, check text to see if it has changed
它會返回錯誤:
File "main.py", line 65, in checkText
if(text.get("1.0", "end-1c") != currentText):
RuntimeError: maximum recursion depth exceeded
除了數以百計的「文件「爲主。 py「,第65行,在checkText'
可能很明顯,我忽略了,但幫助表示讚賞。
只有這些地方的功能是在程序中使用或參考,但只是櫃面,這裏是currentText定義:
myfile = tkFileDialog.askopenfile(title='Open a file', mode='r')
text.delete('1.0', END)
loadedfile = myfile.read()
currentText = myfile.read()
loadedFile將被直接放入文本組件和工作正常,所以我認爲currentText應該與widget中的loadedFile/text相同。
謝謝。
編輯:格式,還我意識到,我只能說currentText = loadedFile
簡化它,但我保持原來的代碼問題
編輯:爲獲取文本
def fileOpen(textView):
try:
myfile = tkFileDialog.askopenfile(title='Open a file', mode='r')
text.delete('1.0', END)
loadedfile = myfile.read()
currentText = loadedFile
currentFile = myfile.name
currentName = currentFile
currentName = currentName.rsplit('/', 1)[-1] #get the 'name.ext' part only
currentName = currentName.rsplit('\\', 1)[-1] #incase you're using windows
currentFileButton.config(text = currentName)
myfile.close()
textView.insert("end", loadedfile)
except:
return
def saveAs(): #define how to save files
try:
global text
t = text.get("1.0", "end-1c")
saveLocation = tkFileDialog.asksaveasfilename()
file1 = open(saveLocation, "w+")
file1.write(t)
file1.close()
except:
return
後全功能:
text = Text(textFrame, bd=0, bg=color, fg=textColor) #make text editor box
看起來遞歸來自'text.get(「1.0」,「end-1c」)!= currentText'。要確認這一點,請嘗試添加一個「else」子句,以便在條件不滿足時打印某些內容。如果你這樣做,是否有東西打印? –
@Rightleg我加了'else:print(「沒有改變」)',同樣的錯誤發生,沒有改變。 –
當然這並不能解決問題,但是「已更改」或「未更改」是否曾打印過? –