使用 root.update 在 python 中打印一次字符串列表
我正在尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 print 语句位于 while 循环内,没有中断条件。如果您正在寻找在满足条件之前运行的东西,那么您需要这样的东西来在满足条件后停止打印。您的语句永远评估为 True,因此它将继续打印 key_press 值。打印语句需要位于条件之后。
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.
您的代码可以解决很多问题。
#1 :使用
["text"]
。这将使您能够以更加干净且性能友好的方式修改小部件中的文本。#2 :根据您的代码片段,您不应该执行
for row in range(len(
,这会导致更多的函数调用以及更多的内存消耗。对))
中的行执行
操作。这会将
row
设置为循环当前迭代的行。例如...此代码位将打印此...
#3 :我添加了一个
convertMapToText
函数来清理和组织您的代码。这并不是完全必要的,但它使它更具可读性。如果您要使用任何类型的循环,最好将算法放入函数中来清理和组织代码。#4:为了防止混淆,当您在列表中放置
for
循环时,它可以让您更有效地填充列表或矩阵(在本例中为列表的列表),而无需调用额外的函数或者增加你的内存占用。#5 :此外,在代码片段的第 6 行,您将收到
IndentationError
错误,因为它与周围行的缩进不匹配,而且它位于while 中
循环块。 Python 依赖于缩进。如果缩进不正确,就会出现错误。至于你问题的主要部分,我假设你的意思是变量
keypress
不断打印。这是因为它处于while
循环中。如果情况并非如此,那么我不确定。您的代码缺少任何其他print
语句。There are mulitple things that could be fixed with your code.
#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. Dofor row in <list>
. This setsrow
to the row the loop is currently iterating through. For example...This code bit would print this...
#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 awhile
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 awhile
loop. If this is not the case, then I am unsure. Your code lacks any otherprint
statements.