2013-05-22 24 views
0

我一直在玩這個相當多的東西,而且我覺得自己弄得一團糟。 我需要一個界面,如果有人在文本字段中輸入一個單詞,它會在包含該單詞旁邊框中的.txt文件中打印任何行。 這是我到目前爲止有:根據用戶輸入構建一個搜索.txt文件的界面,然後在其旁邊打印結果

import Tkinter as tk 
    from Tkinter import * 
    from scipy import * 

    import math 

    def cbc(id, tex): 
     return lambda : callback(id, tex) 

    def callback(id, tex):         
     t = Search(id) 
     tex.insert(tk.END, t) 
     tex.see(tk.END) 
    #def retrieve_input(): 
    # input = self.entn.get("0.0",END) 
    def Search(id): 
     with open("file.txt", "r") as s: 
      searchlines = s.readlines() 
     for i, line in enumerate(searchlines):    
      if entn.get() in line: 
       for line in searchlines[i:i+1]: print line,  
       print 
    top = tk.Tk() 
    tex = tk.Text(master=top)        
    tex.pack(side=tk.RIGHT)         
    bop = tk.Frame() 
    bop.pack(side=tk.LEFT)         
    entn = tk.Entry() 
    entn.pack(side=tk.TOP, fill=Y)       
    tb = "Search" 
    b = tk.Button(bop, text=tb, command=cbc(id, tex)) 
    b.pack() 

    tk.Button(bop, text='Exit', command=top.destroy).pack() 
    top.mainloop() 

我很新的這一點,因爲你可能會說 - 因此,任何幫助將是非常讚賞。

+0

也許更適合@ http://codereview.stackexchange.com/ –

+0

你的問題是什麼?你有什麼需要幫助的? –

+0

@BryanOakley嘿,我真的很新鮮。我不明白爲什麼它不會在文本框中打印結果。我已經設法讓它達到它進行搜索的地步,然後它將它打印在命令提示符中。謝謝 – user2409236

回答

0

你的回調插入Search(id)的結果,但該函數沒有返回任何東西。您需要在該函數中添加一個return語句以返回要在文本窗口小部件中顯示的字符串。

更好的方法可能是將文本小部件的引用傳遞到Search,以便您可以在找到它們時直接插入每個匹配項。

相關問題