2016-04-18 30 views
-1

嗨〜我在python gui中使用tkinter。我的代码是列表框wdiget。我想添加与列表框小部件的过滤器。 但我不知道它没有。我该如何做才能做到这一点。 我想添加过滤器..我不知道这样做.. 请帮我。我不知道make search_data()。如何在tkiter中添加过滤器小部件?

from tkinter import * 

def search_data(): 
    print('d') 


root=Tk() 
dd = Frame(root, borderwidth=0) 
# create the entry widget 
entry_value = StringVar() 
entry = Entry(dd, textvariable=entry_value) 
entry.pack(side=LEFT) # where ever you want it 
Button(dd, text = 'search ', command = search_data).pack(side=LEFT)  
dd.pack() 

scrollbar=Scrollbar(root) 
scrollbar.pack(side=RIGHT,fill=Y) 
mylist=Listbox(root,yscrollcommand=scrollbar.set, width=100, height=15) 
for line in range(100): 
    mylist.insert(END,"This is line number " + str(line)) 
mylist.pack(side=LEFT,fill=BOTH) 
scrollbar.config(command=mylist.yview)  
mainloop() 

回答

0

要有一个过滤器,你需要在你的GUI中有“东西”让你的用户输入过滤器模式。

例如一个Entry小部件,因为我们正在谈论tkinter。

伪代码

# do your imports 

# create the entry widget 
entry_value = StringVar() 
entry = Entry(root, textvariable=entry_value) 
entry.pack() # where ever you want it 

#now add your list box 

#add e.g. a button to apply your filter 

def filter_data(): 
    # ... iterate over your data, check if your filter applies and decide how to proceed 

的按钮:检查回调和数据结构从文档,看看如何可以这样做。

编辑数据的1

储存:

您直接将项目添加到列表中。

mylist.insert(END,"This is line number " + str(line))

这是显示数据的方式 - 是的。 这是可维护的代码吗?不是真的。

要过滤显示的数据,我建议使用一些refresh函数访问存储的数据以显示。

如何实现这一目标? 通常,应该有某种数组,字典或类似的东西来存储数据。也可以是读取和解析数据的文件。

让我给你根据你的代码的例子:

  • 你有一个伪产生for i in range(0,100):

    我想你以后会在读取文件基于您的变量的命名,但将其改为常用的i用作最常用循环中的索引。

    在您的代码中,您可以将数据存储在列表中。

raw_data = range(0,100) 
actual_data = raw_data # this will be used from now on inside your refresh 
def refresh(): 
    # clean your listbox 

    # iterate over the data 
    for line in actual_data: 
     #add it now to the listbox 

def filter(): 
    # get your filter pattern first 
    # we have a string var, so use "get()" 
    filter_pattern = entry_value.get() 
    # now create a new list 
    actual_data = [] 

    # iterate over raw data, check if data matches filter 
    # if it does, append it to actual_data 

    # call refresh now 
    refresh() 

是的,我知道有很多在上面的示例代码丢失。

为什么? 我故意留下了大部分代码。正如Bryan已经提到的,SO是而不是代码写入服务。说实话,我想你没有任何编程经验。让我直说,这不是一件坏事,,但它看起来并不像你做了很多研究。阅读文档,教程,自己尝试。这将帮助您创建代码,软件等。独自一人可能有助于某些编程问题,但不能成为程序员。这是一个决定,没有人可以强迫你成为。

+0

_“你需要用......” - 什么是“......”? –

+0

更新了它 - 希望这可以让它更容易理解。 “sth”应该是“something” – R4PH43L

+0

的缩写,谢谢你的回答。但我的英文不好...你是否显示代码..不是seudo代码.. –