pythonpython-3.xtkintertkinter-text

Tkinter insert function inserts text on to the same line all the time even when I change the line I input


    scoreboard = tkinter.Text(window)
    scoreboard["state"] = "normal"
    scoreboard.pack()

    for count, name, score in zip(range(len(names)), names, scores):
        scoreboard.insert(str(float(count+1)), f"{count+1}. {name}    {score}/10")

    scoreboard["state"] = "disabled"

For example:
If names = ["user2", "user1"]
and scores = [3,0]

It yields: Pic
Where the highlighted text should have been under the first line.

I also verified that str(float(1)) = "1.0" so I don't get why this doesn't work.

Plus, I need this to work with arrays of any size so please don't tell me to insert it manually.

Thanks in advance for the help!


Solution

  • Instead of computing the index, always insert at the end, and be sure to add a newline after each line. If you don't want the extra newline at the end you can always trim it off after the fact.

    for count, name, score in zip(range(len(names)), names, scores):
        scoreboard.insert("end", f"{count+1}. {name}    {score}/10\n")
    scoreboard.delete("end-1c")