I am trying to control the values of variables using the arrow keys on my keyboard to move a square around live on a tkinter canvas. I have no error message but when i run it Python 3.11, the canvas pops up but nothing happens when i press the keys. I'm using
from tkinter import *
from keyboard import *
tk=Tk()
tk.attributes('-fullscreen',True)
canvas=Canvas(tk, width=1366, height=768, background='#fff')
canvas.pack()
colors=[[(255, 255, 255) for i in range(223)] for i in range(321)]
def colr(lis):
a=lis[0]
b=lis[1]
c=lis[2]
if a%256<10:
va='0'+str(a%256)
else:
va=hex(a%256).replace('0x','')
if b%256<10:
vb='0'+str(b%256)
else:
vb=hex(b%256).replace('0x','')
if c%256<10:
vc='0'+str(c%256)
else:
vc=hex(c%256).replace('0x','')
return '#%s'%(va+vb+vc)
def fill_rect(a,b,c,d,e):
a=a+683
b=-b+384
if type(e)==str:
canvas.create_rectangle(a+2,b+2,c+a+2,d+b+2,fill=e,outline='')
else:
canvas.create_rectangle(a+2,b+2,c+a+2,d+b+2,fill=colr(e),outline='')
#Modify the value if the color at pixel x,y
for x in range(c):
for y in range(d):
if (a+x)>=0 and (a+x)<=320 and (b+y)>=0 and (b+y)<=222:
colors[a+x][b+y]=e
xinc=0
yinc=0
fill_rect(xinc,yinc,10,10,[0,0,0])
while True:
if is_pressed("left arrow")==True:
xinc=xinc-1
fill_rect(xinc,yinc,10,10,[0,0,0])
if is_pressed("right arrow")==True:
xinc=xinc+1
fill_rect(xinc,yinc,10,10,[0,0,0])
if is_pressed("up arrow")==True:
yinc=yinc+1
fill_rect(xinc,yinc,10,10,[0,0,0])
if is_pressed("down arrow")==True:
yinc=yinc-1
fill_rect(xinc,yinc,10,10,[0,0,0])
I don't really know how how the "is_pressed()" works so maybe it's just not the good function or even the right library so if you recommand another one i'll gladly take advice.
You may have an easier time using tkinter's built-in event bindings
so instead of checking things like
you would write something like