【问题标题】:Are keyboard library and tkinter compatible?键盘库和 tkinter 兼容吗?
【发布时间】:2022-12-12 12:28:12
【问题描述】:

我正在尝试使用键盘上的箭头键控制变量的值,以便在 tkinter 画布上实时移动一个正方形。我没有错误消息,但是当我运行 Python 3.11 时,画布会弹出,但当我按下按键时没有任何反应。 我在用着

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])

我真的不知道“is_pressed()”是如何工作的,所以也许它不是好的功能,甚至不是正确的库,所以如果你推荐另一个,我会很乐意接受建议。

【问题讨论】:

    标签: python tkinter python-module keyboard-python


    【解决方案1】:

    使用 tkinter 的内置事件绑定可能会更轻松

    所以不要检查像

    if is_pressed("left arrow")==True:
        xinc = xinc-1
        fill_rect(xinc, yinc, 10, 10, [0,0,0])
    

    你会写类似的东西

    def handle_event(event):
        global xinc
        global yinc
        if event.keysym == 'Left':
            xinc=xinc-1
            fill_rect(xinc,yinc,10,10,[0,0,0])
        elif event.keysym == 'Right':
            xinc=xinc+1
            fill_rect(xinc,yinc,10,10,[0,0,0])
        elif event.keysym == 'Up':
            yinc=yinc+1
            fill_rect(xinc,yinc,10,10,[0,0,0])
        elif event.keysym == 'Down':
            yinc=yinc-1
            fill_rect(xinc,yinc,10,10,[0,0,0])
    
    
    # bind the keyboard events to your root window
    # you could also bind them to the canvas: 'canvas.bind(...)'
    tk.bind('<Left>', handle_event)
    tk.bind('<Right>', handle_event)
    tk.bind('<Up>', handle_event)
    tk.bind('<Down>', handle_event)
    

    【讨论】:

      【解决方案2】:

      编写 tkinter 时不应使用 keyboard 模块。 Tkinter 为您处理所有键盘事件。

      在 tkinter 中处理键盘事件的方式是创建按下按键时应调用的函数。然后您可以将该函数绑定到键盘事件。

      例如,在您的代码中,您可以创建如下所示的函数:

      def left_arrow(event):
          global xinc
          xinc=xinc-1
          fill_rect(xinc,yinc,10,10,[0,0,0])
      

      然后,您可以将该函数绑定到画布上的 &lt;Left&gt; 事件以调用该函数:

      canvas.bind("<Left>", left_arrow)
      

      您还必须确保为画布提供键盘焦点,因为默认情况下它不会获得焦点。

      canvas.focus_set()
      

      这不是唯一的解决方案。您还可以将所有键绑定到同一个函数,并检查传入的 event 对象以查看按下了哪个键。您还可以绑定到根窗口或所有窗口(如果您有多个窗口)。

      关于 tkinter 中的事件处理还有很多要学习的内容,大多数 tkinter 教程都涵盖了它,例如 tkdocs.com 中的教程。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多