使用 root.update 在 python 中打印一次字符串列表

发布于 2025-01-18 07:24:38 字数 652 浏览 0 评论 0原文

我正在尝试使用TKINTER在Python中制作一个流氓克隆,我发现使用.update可以做得更好,但不幸的是,当我打印出字符串时,它将其打印出来,然后将其打印

while True:
    print(key_press)
    if key_press == "d":
        current_player_col += 1

map_layout[current_player_col][current_player_row] = "P"
    
    map_layout_printable = ""
    
    for row in range(len(map_layout)):
        for column in range(len(map_layout[row])):
            map_layout_printable += map_layout[row][column]
        map_layout_printable += "\n"


    message = tk.Label(root, text=map_layout_printable)
    message.pack()
    root.update()

出来能够在不反复打印的情况下更新字符串吗?

I am attempting to make a rogue clone in python using Tkinter, I found using .update would work better for what I want to do but unfortunately when I print out the string, it prints it out, and out and out

while True:
    print(key_press)
    if key_press == "d":
        current_player_col += 1

map_layout[current_player_col][current_player_row] = "P"
    
    map_layout_printable = ""
    
    for row in range(len(map_layout)):
        for column in range(len(map_layout[row])):
            map_layout_printable += map_layout[row][column]
        map_layout_printable += "\n"


    message = tk.Label(root, text=map_layout_printable)
    message.pack()
    root.update()

Is there a way of being able to update the string without printing it out over and over again?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我恋#小黄人 2025-01-25 07:24:39

您的 print 语句位于 while 循环内,没有中断条件。如果您正在寻找在满足条件之前运行的东西,那么您需要这样的东西来在满足条件后停止打印。您的语句永远评估为 True,因此它将继续打印 key_press 值。打印语句需要位于条件之后。

data = [1, 2, 3, 4]


while True:
    item = int(input("Enter a number:"))
    if item in data:
        print("Found!")
        break
    else:
        print("Not Found")

You have your print statement inside your while loop without a break condition. If you are looking for something that runs until a condition is met you need something like this to stop it from printing once the condition is met. Your statement perpetually evaluates to True so it will just keep printing whatever key_press value is. The print statement needs to be after the condition.

data = [1, 2, 3, 4]


while True:
    item = int(input("Enter a number:"))
    if item in data:
        print("Found!")
        break
    else:
        print("Not Found")
玻璃人 2025-01-25 07:24:39

您的代码可以解决很多问题。

window = tk.Label(root, text="")
window.pack()


width = 8
height = 8

mp = [["0" for i in range(width)] for i in range(height)]

keypress = None #For now

def convertMapToText(mp1: list) -> str:
    strs = []
    for row in mp1:
        strs.append("".join(row)+"\n")

    return "".join(strs)


while True:
    print(keypress)
    #key input handler here

    window["text"] = convertMapToText(mp)

    root.update()

#1 :使用 ["text"]。这将使您能够以更加干净且性能友好的方式修改小部件中的文本。

#2 :根据您的代码片段,您不应该执行 for row in range(len()) ,这会导致更多的函数调用以及更多的内存消耗。对中的行执行 操作。这会将 row 设置为循环当前迭代的行。例如...

lst = [[0 for i in range(4)] for e in range(4)]
for row in lst:
    print(row)

此代码位将打印此...

[0,0,0,0]
[0,0,0,0]
[0,0,0,0]
[0,0,0,0]

#3 :我添加了一个 convertMapToText 函数来清理和组织您的代码。这并不是完全必要的,但它使它更具可读性。如果您要使用任何类型的循环,最好将算法放入函数中来清理和组织代码。

#4:为了防止混淆,当您在列表中放置 for 循环时,它可以让您更有效地填充列表或矩阵(在本例中为列表的列表),而无需调用额外的函数或者增加你的内存占用。

#5 :此外,在代码片段的第 6 行,您将收到 IndentationError 错误,因为它与周围行的缩进不匹配,而且它位于 while 中 循环块。 Python 依赖于缩进。如果缩进不正确,就会出现错误。

至于你问题的主要部分,我假设你的意思是变量 keypress 不断打印。这是因为它处于 while 循环中。如果情况并非如此,那么我不确定。您的代码缺少任何其他 print 语句。

There are mulitple things that could be fixed with your code.

window = tk.Label(root, text="")
window.pack()


width = 8
height = 8

mp = [["0" for i in range(width)] for i in range(height)]

keypress = None #For now

def convertMapToText(mp1: list) -> str:
    strs = []
    for row in mp1:
        strs.append("".join(row)+"\n")

    return "".join(strs)


while True:
    print(keypress)
    #key input handler here

    window["text"] = convertMapToText(mp)

    root.update()

#1 : Use <labelObject>["text"]. This will let you modify the text in the widget in a much more clean and performance-friendly way.

#2 : Based on your code snippet, you shouldn't do for row in range(len(<list>)), this has significantly more function calls as well as more memory consumption. Do for row in <list>. This sets row to the row the loop is currently iterating through. For example...

lst = [[0 for i in range(4)] for e in range(4)]
for row in lst:
    print(row)

This code bit would print this...

[0,0,0,0]
[0,0,0,0]
[0,0,0,0]
[0,0,0,0]

#3 : I added a convertMapToText function which cleaned up and organized your code. This isn't entirely necessary but it made it much more readable. It is good practice to put algorithms in functions to clean up and organize your code or if your going to use a loop of any sort.

#4 : To prevent confusion, when you put a for loop in a list, it allows you to more efficiently fill a list or matrix (a list of lists in this case) without needing to call extra functions or increase your memory footprint.

#5 : Also, at line 6 in your code snippet, you will get thrown a IndentationError because it doesn't match the indentation of the lines around it as well as it being in a while loop block. Python is dependent on indentation. If you put in improper indentation, you are going to get an error.

As for the main part of your question, I assume you mean the variable keypress keeps printing. This is because it is in a while loop. If this is not the case, then I am unsure. Your code lacks any other print statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文