带有 TKinter 的 Pynput 监听器并比较监听器输出

时间:2021-05-01 05:51:40

标签: python tkinter pynput

我最近问了一个关于 TKinter 的 <KeyPress><KeyRelease> 函数的问题,最终被定向到使用 Pynput 的监听器。

我唯一的问题是侦听器似乎没有输出或返回可以与字符串进行比较的字符。在下面的代码中,第 34-64 行是我的问题 - 特别是第 35、42、49 和 56 行。这些 if 语句应该获取从监听器返回的键,如果它们是相同的,在 TKinter 中移动我的角色。但是,无论我如何格式化 if 语句,它似乎都无法正常工作。

我最初认为 Listener 只返回字符,但使用打印语句进行测试似乎表明它返回的字符两边都带有单引号。

关于如何将 Listener 中的项目与字符串进行比较的任何想法?非常感谢!

# Import Tkinter
from tkinter import *
from pynput import keyboard

# Create Window
root = Tk()
canvas = Canvas(root, width=1000, height=1000)
canvas.pack()

# Create Background and Walls
background = canvas.create_rectangle(0, 0, 1000, 1000, fill='gray')
ground = canvas.create_rectangle(0, 840, 1000, 1000, fill='black')
leftwall = canvas.create_rectangle(0, 0, 30, 1000, fill='black')
rightwall = canvas.create_rectangle(970, 0, 1000, 1000, fill='black')
skybox = canvas.create_rectangle(0, 0, 1000, 25, fill='black')

# Create Character Models
character = canvas.create_rectangle(0, 0, 50, 50, fill='lightblue')

# Set Character start position
canvas.move(character, 500, 500)

# Get Character Position
x0, y0, x1, y1 = canvas.coords(character)
xpos = ((x1 - x0) / 2) + x0
ypos = ((y1 - y0) / 2) + y0

# Global Variables
contact = False
throwvar = ""


# Define Movements
def move_character(whatkey):
    if whatkey == "'w'":
        canvas.move(character, 0, -10)
        print("Moved up")
        root.after(20, move_character, whatkey)
        xbounds()
        ybounds()

    elif whatkey == "'a'":
        canvas.move(character, -10, 0)
        print("Moved left")
        root.after(20, move_character, whatkey)
        xbounds()
        ybounds()

    elif whatkey == "'d'":
        canvas.move(character, 10, 0)
        print("Moved right")
        root.after(20, move_character, whatkey)
        xbounds()
        ybounds()

    elif whatkey == "'s'":
        canvas.move(character, 0, 10)
        print("Moved down")
        root.after(20, move_character, whatkey)
        xbounds()
        ybounds()

    else:
        print("Listener doesn't work :(")


# Run Detection for if a key is released after being held
def on_press(key):
    whatkey = key
    move_character(whatkey)
    print('Pressed Key %s' % key)


def on_release(key):
    print('Released Key %s' % key)


# Apply constant downwards force while above y co-ords of 'ground'
def gravity():
    x0, y0, x1, y1 = canvas.coords(character)
    ypos = ((y1 - y0) / 2) + y0
    canvas.move(character, 0, 10)
    if ypos >= 800:
        root.after(20, anti)
    elif ypos <= 800 and contact == False:
        root.after(20, gravity)
        xbounds()
        ybounds()
    else:
        print("Gravity Error")


# Stop gravity when hitting 'ground'
def anti():
    x0, y0, x1, y1 = canvas.coords(character)
    ypos = ((y1 - y0) / 2) + y0
    canvas.move(character, 0, 0)
    if ypos <= 800:
        root.after(20, gravity)
    else:
        root.after(20, anti)
    xbounds()
    ybounds()


# Set bounds of screen, left/right
def xbounds():
    x0, y0, x1, y1 = canvas.coords(character)
    if x0 <= 25:
        print("Horizontal Contact, Left!")
        canvas.coords(character, 25, y0, 75, y1)
    elif x1 >= 975:
        print("Horizontal Contact, Right!")
        canvas.coords(character, 900, y0, 950, y1)


def ybounds():
    x0, y0, x1, y1 = canvas.coords(character)
    if y0 <= 25:
        print("Vertical Contact, Upper!")
        canvas.coords(character, x0, 25, x1, 75)
    elif y1 >= 975:
        print("Vertical Contact, Lower!")
        canvas.coords(character, x0, 925, x1, 975)


# Implement Gravity
root.after(500, gravity)


# Bind keys
def listen_to_me():
    with keyboard.Listener(on_press=on_press, on_release=on_release, move_character=move_character) as listener:
        root.mainloop()
        listener.join()


# Focus
root.focus_set()

# Start input code
listen_to_me()

1 个答案:

答案 0 :(得分:0)

通过获取 Listener 返回的字符串、找到所述返回的 index[1] 并使用它来移动来找到我的答案。还删除了我的移动函数中的 root.after(20, move_character, whatkey),因为它会无限递归,这没有帮助。