【问题标题】:How to create a search bar to search keywords in my Tkinter table?如何创建搜索栏以在我的 Tkinter 表中搜索关键字?
【发布时间】:2022-12-06 18:35:10
【问题描述】:

我正在制作一个用户界面。我用我的 csv 中的数据创建了一个表,并想实现一个搜索栏,您可以在其中输入关键字,它将输出包含关键字的行。 TIA!这是我的代码和csv

from tkinter import *
import tkinter.ttk as ttk
import csv

root = Tk()
root.title("Python - Import CSV File To Tkinter Table")
width = 900
height = 600
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)


TableMargin = Frame(root, width=500)
TableMargin.pack(side=TOP)
scrollbarx = Scrollbar(TableMargin, orient=HORIZONTAL)
scrollbary = Scrollbar(TableMargin, orient=VERTICAL)
tree = ttk.Treeview(TableMargin, columns=("Link", "Name", "Email"), height=400, selectmode="extended", yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('Link', text="Link", anchor=W)
tree.heading('Name', text="Name", anchor=W)
tree.heading('Email', text="Email", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=200)
tree.column('#2', stretch=NO, minwidth=0, width=200)
tree.column('#3', stretch=NO, minwidth=0, width=300)
tree.pack()

with open('output.csv') as f:
    # reader = csv.DictReader(f, delimiter=',')
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        if len(row) == 0:
            continue
        link: str = row[0]
        name: str = row[1]
        email: str = row[2]
        # specialty: str = row[3]
        tree.insert("", 0, values=(link, name, email))
        

#============================INITIALIZATION==============================
if __name__ == '__main__':
    root.mainloop()

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:
    1. 创建一个 Entry 小部件,以及一个表示 Entry 小部件内容的字符串变量,如下所示:
      Searchbar = Entry(root)
      Searchbar.pack()
      SearchString = StringVar(Searchbar, "")
      Searchbar.config(textvariable = SearchString)
      
      1. 创建一个标签,您可以使用该标签显示带有关键字的相关行,如下所示:
      SearchResultBar = Label(root)
      SearchResultBar.pack()
      
      1. 创建一个函数,您可以使用该函数在用户输入关键字时显示搜索结果。您通过调用 SearchString.get() 获取关键字的值
      def DisplaySearchResult():
        Keyword = SearchString.get
        {write some code to search your csv data and get a string to describe the search result, which we'll call SearchResult}
        SearchString.set(SearchResult)
      
      1. (可选)将该函数绑定到搜索栏,以便该函数在有人单击 enter 或搜索栏失去焦点时运行,如下所示:
      Searchbar.bind('<Return>', DisplaySearchResult, add = '+')
      Searchbar.bind('<FocusOut>', DisplaySearchResult, add = '+')
      

      应该这样做。祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 2017-11-03
      相关资源
      最近更新 更多