Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading