[英]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"]如果名称 = ["user2", "user1"]
and scores = [3,0]分数 = [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.我还验证了 str(float(1)) = "1.0" 所以我不明白为什么这不起作用。
Plus, I need this to work with arrays of any size so please don't tell me to insert it manually.另外,我需要它与任何大小的 arrays 一起使用,所以请不要告诉我手动插入它。
Thanks in advance for the help!先谢谢您的帮助!
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.