首页 > 解决方案 > 使用 tkinter 搜索 csv 文件

问题描述

好的,所以我能够使用 treeview 很好地将所有 csv 文件放入 tkinter

这是代码

from tkinter import *
from tkinter import ttk
import csv
 
 
app = Tk()
app.title('Test')
app.geometry('600x200')
 
file = r'C:\Users\Home\Documents\studying\newproject\moviedata.csv'
 
f = open(file, 'r')
csvreader = csv.reader(f)
csvreader_list = list(csvreader)
 
# print(csvreader_list)
 
 
tv = ttk.Treeview(app, columns=('col_1', 'col_2', 'col_3'), show='headings')
tv.column('col_1', minwidth=0, width=400)
tv.column('col_2', minwidth=0, width=100)
tv.column('col_3', minwidth=0, width=100)
 
tv.heading('col_1', text='TITLE')
tv.heading('col_2', text='YEAR')
tv.heading('col_3', text='IMDBID')
 
tv.pack()
 
 
for (i, n, f) in csvreader_list:
    tv.insert('', 'end', values=(i,n,f))
    
app.mainloop()

我知道如何输入并给它一个 stringvar 以便稍后调用搜索按钮

但我不知道如何正确迭代它,以便用户可以输入例如按年份搜索:2020,它将打印 2020 年制作的所有电影 这里

标签: pythoncsvtkinter

解决方案


好的,如果有人正在寻找这样的解决方案,我尝试并尝试直到它起作用,这就是我到目前为止所拥有的,我将对其进行编辑以使其更清洁

from tkinter import *
import tkinter as tk
from tkinter import ttk
import csv
 
 
app = tk.Tk()
app.title('Test')
app.geometry('800x500')
x=StringVar()
y=StringVar()
z=StringVar()
 
file = r'C:\Users\Home\Documents\studying\newproject\moviedata.csv'
 
f = open(file, 'r')
csvreader = csv.reader(f)
csvreader_list = list(csvreader)
 
# print(csvreader_list)


label1=tk.Label(app,text="By Title:",font="Helvetica 12",fg="white",bg="#460B05")
label1.place(x=10,y=10)
search1=tk.Entry(app,textvariable=x)
search1.place(x=80,y=10,height=23,width=200)

label2=tk.Label(app,text="By Year:",font="Helvetica 12",fg="white",bg="#460B05")
label2.place(x=300,y=10)
search2=tk.Entry(app,textvariable=y)
search2.place(x=370,y=10,height=23,width=200)

label3=tk.Label(app,text="By ID:",font="Helvetica 12",fg="white",bg="#460B05")
label3.place(x=580,y=10)
search3=tk.Entry(app,textvariable=z)
search3.place(x=630,y=10,height=23,width=200)




 
 
tv = ttk.Treeview(app, columns=('col_1', 'col_2', 'col_3'), show='headings')
tv.column('col_1', minwidth=0, width=400)
tv.column('col_2', minwidth=0, width=100)
tv.column('col_3', minwidth=0, width=100)
 
tv.heading('col_1', text='TITLE')
tv.heading('col_2', text='YEAR')
tv.heading('col_3', text='IMDBID')
 
tv.place(x=100,y=200)
 
def search():
    
    tv.delete(*tv.get_children())
    word=x.get().title()
    word1=y.get()
    word2=z.get()
    if x.get():
        for (i, n, f) in csvreader_list:
            if word in i:
                tv.insert('', 'end', values=(i,n,f))
    elif y.get():
        for (i, n, f) in csvreader_list:
            if word1 in n:
                tv.insert('', 'end', values=(i,n,f))
    elif z.get():
        for (i,n,f) in csvreader_list:
            if word2 in f:
                tv.insert('', 'end', values=(i,n,f))
    else:
        for (i,n,f) in csvreader_list:
            tv.insert('', 'end', values=(i,n,f))
    search1.delete(0, 'end')
    search2.delete(0, 'end')
    search3.delete(0, 'end')
        

searchbutton=tk.Button(app,text="Search",fg="black",command=search,anchor="center",justify=CENTER)
searchbutton.place(x=1,y=50,width=898)
app.mainloop()

推荐阅读