我有一个标签,我想要为空,直到按下按钮,搜索csv文件以从输入名称的同名条目中查找分数。发现显示在标签中的任何值,当前只有第一个值由列表显示,谢谢。
entSearch = Entry(window)
lblSearch = tkinter.Label(window, text="Search for your previous scores!")
btnSearch = Button(window, text="Search!",command=search)
lblSearched = tkinter.Label(window, text="")
def search():
file1=open("scores.csv","r")
csvreader=csv.reader(file1)
for x in csvreader:
if entSearch.get() == x[0]:
results = x[0]+" "+x[1]+"\n"
lblSearched.config(text=results)
答案 0 :(得分:0)
您可以将所有文字连接起来,然后将其放入Label
searched_text = entSearch.get()
full_text = ''
for row in csvreader:
if row[0] == searched_text:
# with '\n'
full_text += "{} {}\n".format(row[0], row[1])
lblSearched.config(text=full_text)
或join()
(最后一行后不会添加\n
)
searched_text = entSearch.get()
all_lines = []
for row in csvreader:
if row[0] == searched_text:
# WITHOUT '\n'
all_lines.append("{} {}".format(row[0], row[1]))
full_text = '\n'.join(all_lines)
lblSearched.config(text=full_text)
在您当前的代码中,您可以使用
lblSearched['text'] += results
或
lblSearched.config(text=lblSearched.cget('text') + results)
但它的可读性较差。