简体   繁体   English

Tkinter 插入 function 始终将文本插入到同一行,即使我更改输入的行也是如此

[英]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.

相关问题 当我将 update() 与 tkinter 一起使用时,我的 Label 会写另一行而不是重写相同的文本 - When I use update() with tkinter my Label writes another line instead of rewriting the same text 当我在tkinter中删除文本框时,总是留空行 - When I delete Text box in tkinter, a blank line is always left Python3 Tkinter文本小部件在同一行上插入 - Python3 Tkinter Text Widget INSERT on same line 使用Tkinter对应用程序进行编程需要添加具有相同功能的按钮。 如何确定我单击了哪个按钮并自动更改行? - Using Tkinter to program an application need add buttons with same function. How to identify which button I clicked and change the line automatically? python 在文本 tkinter 中插入字符串列表时在每一行添加 {} - python adds { } in each line when insert string list in text tkinter 如何在 Tkinter Treeview 中插入新行? - How can I insert a new line in Tkinter Treeview? Python 只读最后一行,但我想要所有的,即使是同名的 - Python read last line only but I want all even with the same name 如何在Tkinter中获取文本结束位置的行和列? - How do I get the line and column of the end position of text in Tkinter? 如何在 tkinter python 中没有 after() function 的情况下在特定时间后更改 label 文本? - How can I change a label text after a specific time without after() function in tkinter python? 如何同时在tkinter画布上更改和移动图像? - How can I change and move an image on a tkinter Canvas at same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM