【问题标题】:Pynput Listener with TKinter and comparing Listener OutputPynput 监听器与 TKinter 并比较监听器输出
【发布时间】:2021-07-24 08:24:35
【问题描述】:

我最近问了一个关于 TKinter 的 <KeyPress><KeyRelease> 函数的问题,最终被引导使用 Pynput 的 Listener。

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

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

关于如何将 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()

【问题讨论】:

  • 为什么不只使用 tkinter 来检测密钥?使用 tkinter,您只需执行 event.keysym 即可获得您想要的。
  • 我想不出你需要 pynput 的理由。 Tkinter 的事件系统非常灵活。你想完成什么你认为你需要pynput?

标签: python tkinter pynput


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-02-11
    • 2019-04-19
    • 2021-10-28
    • 2020-05-24
    • 1970-01-01
    • 2023-03-14
    • 2012-05-25
    • 2013-12-24
    • 2014-12-13
    相关资源
    最近更新 更多