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.