【问题标题】:how can i add filter widget in tkiter?如何在 tkinter 中添加过滤器小部件?
【发布时间】:2016-04-19 18:19:58
【问题描述】:

嗨~我在 python gui 中使用 tkinter。我的代码是列表框 wdiget。我想添加带有列表框小部件的过滤器。 但我不知道让它不。我该怎么做才能做到这一点。 我想添加过滤器..我不知道那样做.. 请帮帮我。我不知道make search_data()。

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()

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    要拥有过滤器,您需要在 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 不是代码编写服务。老实说,我想你没有任何编程经验。让我直截了当地说,这不是一件坏事,但看起来你并没有做很多研究。阅读文档、教程,自己尝试一些东西。这将帮助您创建代码、软件等。仅 SO 可能有助于解决某些编程问题,但对成为程序员没有帮助。这是一个决定,任何人都不能强迫你这样做。

    【讨论】:

    • “你需要使用 sth” - 什么是“sth”?
    • 更新了它——希望这能让它更容易理解。 “sth”应该是“something”的缩写
    • 感谢您的回答。但是我的英语很差...你给我看代码吗..不是伪代码..
    • @R4PH43L 我修复了一些代码。但我不知道 make search_data() 。请帮帮我
    猜你喜欢
    • 2015-01-19
    • 2017-08-25
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多