【问题标题】:Can't move graphics using graphics.py and pynput无法使用 graphics.py 和 pynput 移动图形
【发布时间】:2019-09-07 12:42:29
【问题描述】:

我正在尝试使用 pynput 在 python 中移动一个点,但它没有移动

我已经尝试让keyboard.press("W") 将 pointx 和 pointy 增加 10

    from graphics import *
    from keyboard import *
    from pynput import *

    keyboard = keyboard.Controller()
    pointx = 250
    pointy = 250

    win = GraphWin("test", 500, 500)
    pt = Point(pointx, pointy)
    pt.draw(win)

    while keyboard.press("w"):
        pt.move(10, 10)
        pt.draw(win)

没有错误信息

【问题讨论】:

  • 请在您的帖子中实际提出问题,否则您很可能不会得到答案:)
  • 另外,你的代码很混乱。 keyboard.Controller是从pynput导入的,它的全称是pynput.keyboard.Controllerfrom keyboard import * 完全未使用,可以删除。通过创建具有相同名称的 keyboard 变量来覆盖 keyboard 包。然后,这可能是真正的问题:keyboard.press检查按键,它生成它们。

标签: python-3.x graphics pynput


【解决方案1】:

第一件事:pynputkeyboard 库做同样的事情,所以你只需要使用其中一个。由于keyboard 在Linux 上似乎需要root 权限,我建议使用pynput

不要重复使用相同的名称。 keyboard 已经是一个包了,不要用它作为变量名。

keyboard.Controller() 用于控制键盘,而不是用于读取它。您可能正在寻找的是keyboard.Listener

使用keyboard.Listener,您无法直接检查按键,而是在按键被按下或释放时收到通知。这些通知(=回调)函数必须在其构造函数中提供给keyboard.Listener
然后,您可以在按键被按下时直接应用操作,或者您可以在全局变量中跟踪当前按键状态,如下所示:

# The global dict that keeps track of the keyboard state
key_state = {}

# The function that gets called when a key gets pressed
def key_down(val):
    global key_state
    key_state[val] = True

# The function that gets called when a key gets released
def key_up(val):
    global key_state
    key_state[val] = False

# Initializes the keyboard listener and sets the functions 'key_down' and 'key_up'
# as callback functions
keyboard_listener = keyboard.Listener(on_press=key_down, on_release=key_up)
keyboard_listener.start()

然后我们可以在我们的程序中检查一个键是否被按下:

if key_state.get(keyboard.KeyCode(char='w')):

整个程序如下所示:

from graphics import *
from pynput import *

import time


pointx = 250
pointy = 250

win = GraphWin("test", 500, 500)
pt = Point(pointx, pointy)
pt.draw(win)


# The global dict that keeps track of the state of 'w'
key_state = {}

# The function that gets called when a key gets pressed
def key_down(val):
    global key_state
    key_state[val] = True

# The function that gets called when a key gets released
def key_up(val):
    global key_state
    key_state[val] = False

# Initializes the keyboard listener and sets the functions 'key_down' and 'key_up'
# as callback functions
keyboard_listener = keyboard.Listener(on_press=key_down, on_release=key_up)
keyboard_listener.start()


# Continuously loop and update the window (important so it doesn't freeze)
while win.isOpen():
    win.update()
    time.sleep(0.01)

    # Little bit of trickery: 
    # We combine the check if the key exists and if its value is 'true' in one
    # single operation, as both 'None' and 'False' are the same value for 'if'.
    if key_state.get(keyboard.KeyCode(char='w')):
        pt.move(10, 10)

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2013-06-04
    相关资源
    最近更新 更多