Query data in a txt file with python Tkinter

Asked

Viewed 128 times

-2

I’m creating a simple system to query employee data stored in a file txt.

def read_from_file(): 
    with open('dados.txt') as file:  
        for line in file:  
            line = line.rstrip('\n')
            nome, matricula, supervisor, email, chave, local, centrodecusto = line.split('/') 
            base_dados[nome] = matricula, supervisor, email, chave, local, centrodecusto 

def write_to_file(nome_data, matricula_data, supervisor_data, email_data, chave_data, local_data, centrodecusto_data):
    with open('dados.txt') as file:   
        file.write('\n' + nome_data + '/' + matricula_data + '/' + supervisor_data + '/' + email_data + '/' + chave_data + '/' +  local_data + '/' + centrodecusto_data )

read_from_file() 
while True: 
    query_nome = simpledialog.askstring('Dados empregados', 'Digite o nome do empregado: ') 
    if query_nome in base_dados:
        result = base_dados[query_nome]
        messagebox.showinfo ('Answer',  
                             'teste' + query_nome + 'teste' + result + 'teste')  

root.mainloop()  

But code returns me the following error:

File "path", line 24, in 'test' + query_name + 'test' + result + 'test') Typeerror: must be str, not tuple

Could you help me?

  • result = base_dados[query_nome] is returning a tuple.

1 answer

1

The error message is being very specific, you are concatenating a tuple (tuple) into a string, this is not possible.


To fix this very punctually, you can turn your tuple into a string, just call the function str with the tuple as parameter:

messagebox.showinfo ('Answer','teste' + query_nome + 'teste' + str(result) + 'teste')

This way the data of your tuple is converted to string, being displayed between parentheses and separated by comma, something similar to this:

('000001', 'Daniel', '[email protected]', '000001', 'São Paulo', '000001')

Below your example with the changes so that the data is displayed without generating exception:

import tkinter as tk
from tkinter import simpledialog
from tkinter import messagebox

base_dados = {}

def read_from_file():
    with open('dados.txt') as file:
        for line in file:  
            line = line.rstrip('\n')
            nome, matricula, supervisor, email, chave, local, centrodecusto = line.split('/')
            base_dados[nome] = matricula, supervisor, email, chave, local, centrodecusto 

def write_to_file(nome_data, matricula_data, supervisor_data, email_data, chave_data, local_data, centrodecusto_data):
    with open('dados.txt') as file:
        file.write('\n' + nome_data + '/' + matricula_data + '/' + supervisor_data + '/' + email_data + '/' + chave_data + '/' +  local_data + '/' + centrodecusto_data )

root = tk.Tk()
read_from_file()

while True:
    query_nome = tk.simpledialog.askstring(parent=root, title='Dados empregados', prompt='Digite o nome do empregado: ')

    if not query_nome:
        break

    if query_nome in base_dados:
        result = base_dados[query_nome]
        messagebox.showinfo ('Answer',  
                            'Nome: ' + query_nome + ' - Dados: ' + str(result) + '.')

root.mainloop()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.