2016-11-11 39 views
-1

我試圖用Tkinter在Python中創建一些文本編輯器,但我遇到了一個問題。Python(Tkinter) - 文本編輯器 - 查找函數

我一直在試圖爲我的文本編輯器做一個查找功能,但到目前爲止它不太好。

這是我的atm。

def find(self): 
    target = askstring('Mainwindow','Search string') 
    if target: 
     where = self.aText.search(target,INSERT,END) 
     if where: 
      print(where) 
      pastit = where + ('+%dc' % len(target)) 
      self.aText.tag_add(SEL, where, pastit) 
      self.aText.mark_set(INSERT, pastit) 
      self.aText.see(INSERT) 
      self.aText.focus() 

當我運行這個它表明這一點:

AttributeError的:「_tkinter.tkapp」對象有沒有屬性「aText」

我可以打開查找窗口,但它不會做什麼我想,那當然是找到單詞。

任何想法?

+0

這是不足以實際展示問題的代碼。據推測,你從來沒有真正將你的文本字段分配給「self.aText」。 – jasonharper

+0

如果python告訴你你的應用沒有名爲'aText'的屬性,那麼你需要相信它。既然我們看不到你認爲你在哪裏定義它,我們不能說出什麼問題。 –

回答

0

這是一個MCVE這個例外,解決方案註釋掉了。

import tkinter as tk 
root = tk.Tk() 
# root.aText = tk.Text(root) 
print(root.aText) 

這將產生

Traceback (most recent call last): 
    File "F:\Python\mypy\tem.py", line 3, in <module> 
    root.aText 
    File "C:\Programs\Python36\lib\tkinter\__init__.py", line 2095, in __getattr__ 
    return getattr(self.tk, attr) 
AttributeError: '_tkinter.tkapp' object has no attribute 'aText' 

註釋該行並獲得(在即將到來的3.6.0b4)

.!text 

現在發現,你有一個錯字,如果不是缺少一行。

注意:初學者常犯的錯誤是在運行和測試之前輸入太多的代碼。在我看來,你不應該編寫if where:代碼,直到你知道where =代碼的工作。這是假設失敗是在where =聲明中,而不是以前運行的代碼。