Search code examples
pythontkintertextbox

python adds { } in each line when insert string list in text tkinter


I have a problem with python when I try to show a list in a ScrolledText box.

My code opens a .txt file, reads each line and shows it in the ScrolledText box. Below you can see a little part of my code

from tkinter import *
from tkinter import ttk
from tkinter.scrolledtext import *
from tkinter.filedialog import askopenfilename
from tkinter import filedialog

def cargar_trazas():
    lista_trazas_sistema = []
    lista_trazas_subsistema = []
    global contenido_trazas
    
    path_trazas = askopenfilename( filetypes = (("all files","*.*"),("text files","*.txt"),("log files","*.log")))
    archivo_trazas = open(path_trazas,'r')
    contenido_trazas = archivo_trazas.readlines()
    fichero_trazas_text.insert(INSERT, contenido_trazas)


fichero_trazas_text = ScrolledText(miFrameTextoTrazas, width=130, heigh=10)
fichero_trazas_text.grid(row=0, column=1, padx=10, pady=10)

The problem is that when the file is showed in the scrolled text in each line, python adds { }.

enter image description here

Why does python do this? In the list called contenido_trazas these characters do not appear { } and are only seen the ScrolledText box

Is there an option to discard these characters so they will not appear?

thanks


Solution

  • It is doing that because you are passing a list or tuple to insert, but it expects a string. tkinter is just a wrapper around a tcl/tk interpreter, and tcl uses curly braces to preserve the list structure when converting lists to strings. Therfore, you get curly braces (or sometimes backslashes) when passing a list into a widget method that expects text.

    The simplest solution is to use read() rather than readlines().