[英]Returning different Labels from a For Loop inside Tkinter (python)
我需要一些有關將名稱搜索到文件中的代碼的幫助,然后返回所有相關名稱。 在提示中正確打印了一個列表,但在 Tkinter GUI 中,包含名稱的標簽重復了最后一個值。
示例:搜索 Luiz Fernando ->
在提示中:
bruno@servidora:~$ python teka.py
L 31 LUIZ FERNANDO GONÇALVES
L16 9 LUIZ FERNANDO SOUZA CARVALHO
L18 3 LUIZ FERNANDO CAVALHEIRO
L18 4 LUIZ FERNANDO S. DA SILVA
L19 10 LUIZ FERNANDO BELUZZO DA SILVA
在圖形用戶界面中:
編碼:
from Tkinter import *
#search func
def busca():
db = open('HD-Secretaria/morto.csv','r') #database file for name searching
x = entrada.get().upper()
for lines in db:
if x in lines:
print lines
result.set(lines)
Label(app, textvariable = result).pack()
#create window
app = Tk()
app.title('Gerenciador de arquivo morto')
app.geometry('500x400+200+100')
Label(app, text = 'Gerenciador de arquivo morto').pack() #title label
Label(app, text = 'Nome do cidadao').pack() #text label
entrada = Entry(app)
entrada.pack() #textbox
Button(app, text = 'Buscar', command = busca).pack() #button
#result label
result = StringVar()
result.set('')
l = Label(app, textvariable = result).pack()
app.mainloop()
我需要 GUI 來重新發送提示中的數據。
幫幫我吧xD
您的錯誤是您對所有標簽使用了一個變量,而一個變量只能包含一個字符串。
您根本不需要textvariable
選項。 只需對標簽中的字符串進行硬編碼:
Label(app, text=lines)
或者,如果lines
實際上是一個列表:
Label(app, text=" ".join(lines))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.