使用tkinter在CSV文件中搜索

时间:2020-10-06 08:01:05

标签: python csv tkinter

好吧 我能够使用treeview在tkinter中很好地适合所有csv文件

这是代码

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年制作的所有电影 here

1 个答案:

答案 0 :(得分:0)

好吧,我尝试了直到尝试成功 如果有人正在寻找这样的解决方案 这是我到目前为止要编辑的内容,以使其更干净

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