2013-05-22 26 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,以便您可以在找到它们时直接插入每个匹配项。

相关问题