Python 3.x 无法使用graphics.py和pynput移动图形

Python 3.x 无法使用graphics.py和pynput移动图形,python-3.x,graphics,pynput,Python 3.x,Graphics,Pynput,我试图使用pynput在python中移动一个点,但它不移动 我试着让keyboard.pressW将pointx和pointy增加10 从图形导入* 从键盘导入* 从pynput导入* 键盘=键盘。控制器 点X=250 尖头Y=250 win=图形界面,500,500 pt=点x,点y 德拉温角酒店 同时按键盘。按w: 第10、10页 德拉温角酒店 没有错误消息首先要做的事情是:pynput和键盘库做相同的事情,所以您只需要使用其中一个。由于键盘在Linux上似乎需要根权限,我建议使用pynp

我试图使用pynput在python中移动一个点,但它不移动

我试着让keyboard.pressW将pointx和pointy增加10

从图形导入* 从键盘导入* 从pynput导入* 键盘=键盘。控制器 点X=250 尖头Y=250 win=图形界面,500,500 pt=点x,点y 德拉温角酒店 同时按键盘。按w: 第10、10页 德拉温角酒店
没有错误消息

首先要做的事情是:pynput和键盘库做相同的事情,所以您只需要使用其中一个。由于键盘在Linux上似乎需要根权限,我建议使用pynput

不要使用相同的名字两次。键盘已经是一个包,不要将其用作变量名

键盘。控制器是用来控制键盘的,而不是用来读取键盘。您可能正在寻找的是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()
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)
然后,我们可以在程序中检查按键是否按下:

if key_state.get(keyboard.KeyCode(char='w')):
整个程序将如下所示:

# 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()
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)

请在你的帖子里问一个问题,否则你很可能得不到答案:而且,你的代码很混乱。控制器是从pynput导入的,其全名为pynput.keyboard.Controller。从键盘导入*完全未使用,可以删除。通过创建具有相同名称的键盘变量,键盘包将被覆盖。然后,这可能是真正的问题:keyboard.press不检查按键,而是生成它们。