python - 使用 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 以便稍后调用搜索按钮
解决方案
好的,如果有人正在寻找这样的解决方案,我尝试并尝试直到它起作用,这就是我到目前为止所拥有的,我将对其进行编辑以使其更清洁
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()
推荐阅读
- node.js - 响应中“Access-Control-Allow-Credentials”标头的值为“”,必须为“真”
- git - 如何在 vscode 提交消息中自动列出冲突文件
- html - 管理员创建新产品时如何创建表单以对图像进行排序?
- python - 使用 TF-IDF 的搜索栏
- javascript - 单击时无法禁用按钮
- javascript - 样式属性中没有变量
- react-native - react native recyclerlistview指标和性能
- url - Ansible 从 URL 获取二进制文件的哈希值
- sql - 基于 PowerBI 中用户输入的 SQL 动态查询
- python - 打印到控制台时路径正确,但在 pandas .to_csv 方法中不正确