0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Or have python iterate over the return from readlines() and insert each string individually
@PranavHosangadi: yes, of course, but that would be much slower and less efficient.
Agreed. Unless OP has a good reason for needing a list of lines instead of a single string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.