bruno
bruno

Reputation: 375

Returning different Labels from a For Loop inside Tkinter (python)

I need some help with a code for searching names into a file, then return all related names. In the prompt a list is correctly printed, but in the Tkinter GUI the Labels containing the names repeat the last value.

example: Search for Luiz Fernando ->

In the prompt:

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

in the GUI:

GUI image printscreen


the code:

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

I need the GUI to resent the data as in the prompt.

help me guys xD

Upvotes: 1

Views: 457

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385900

Your mistake is that you're using a single variable for all of the labels, and a single variable can only hold a single string.

You don't need the textvariable option at all. Just hard-code the string in the label:

Label(app, text=lines)

Or, if lines is actually a list:

Label(app, text=" ".join(lines))

Upvotes: 1

Related Questions