Home > database >  Python Tkinter: How do you loop through a list and display items on a new line in a new root
Python Tkinter: How do you loop through a list and display items on a new line in a new root

Time:10-25

I am building a translation app using tkinter, and I am trying to get the translated results to appear on a new line in a new root window. So if the user translates


one
two
three

The new tkinter root window should show


uno
dos
tres

Here is the function that displays the translated words

def display_translated(text_to_trans):
    root2 = Tk() # new root window 
    root2.title('Translated text')
    root2.geometry('200x200')
    label2 = Label(root2, text='This is the translated text') # label to explain what the root window is showing 
    label2.pack()
    display_words = [] # list of empty words that will be appended with the list of translated words that was passed to the function 
    for item in text_to_trans: 
        display_words.append(item.text)
    textbox1 = Text(root2, height=10, width=20, font=("Arial", 20), bg='yellow')
    textbox1.pack()
    textbox1.insert(1.0, display_words)

My guess is that the issue is with the way that I am displaying the words on the screen (the display_words list), but I am unsure of a better way to do this to get the translated words.

EDITED: When I am getting instead is

uno dos tres.

CodePudding user response:

You need to explicitly insert a newline between each element. The simplest way is with join:

textbox1.insert("1.0", "\n".join(display_words))
  • Related