2015-02-05 32 views
0

最近我一直在研究GUI python純文本編輯器。該代碼調用此功能無法獲取python 2.7 tkinter文本編輯器查找功能工作

def Find(): 
    win = Toplevel() 
    Label(win, text="Find:").pack() 
    e1 = Entry(win) 
    e1.pack() 
    Button(win, text="Find Me!!!!", command=win.destroy).pack() 
    targetfind = e1.get() 
    print targetfind 
    if targetfind: 
     where = textPad.search(targetfind, INSERT, END) 
     if where: 
      print where 
      pastit = where + ('+%dc' % len(targetfind)) 
      #self.text.tag_remove(SEL, '1.0', END) 
      textPad.tag_add(SEL, where, pastit) 
      textPad.mark_set(INSERT, pastit) 
      textPad.see(INSERT) 
      textPad.focus() 

但是,我無法讓它工作。我搜索了互聯網尋找任何可能幫助我實現查找功能的東西,但是我沒有成功找到一個可行的方法。非常感謝在實現查找功能方面的任何幫助。

我使用python 2.7.7,Tkinter的,而我在Windows 7 `

回答

3

運行此它不工作,因爲你創建e1,然後一納秒後,做targetfind = e1.get()。用戶沒有反應在那納秒內輸入查詢。您所有以targetfind = e1.get()開頭的代碼都需要在您的「查找」按鈕的command中執行。

def Find(): 
    def find_button_pressed(): 
     targetfind = e1.get() 
     print targetfind 
     if targetfind: 
      where = textPad.search(targetfind, INSERT, END) 
      if where: 
       print where 
       pastit = where + ('+%dc' % len(targetfind)) 
       #self.text.tag_remove(SEL, '1.0', END) 
       textPad.tag_add(SEL, where, pastit) 
       textPad.mark_set(INSERT, pastit) 
       textPad.see(INSERT) 
       textPad.focus() 
     win.destroy() 
    win = Toplevel() 
    Label(win, text="Find:").pack() 
    e1 = Entry(win) 
    e1.pack() 
    Button(win, text="Find Me!!!!", command=find_button_pressed).pack() 
+0

謝謝你,你的回答是有效的,很明顯,它很好地解釋了。如果我有足夠的聲望,我會投票。 – 2015-02-05 23:14:53