Pynput Listener 与 TKinter 并比较 Listener Output
Pynput Listener with TKinter and comparing Listener Output
我最近问了一个关于 TKinter 的 <KeyPress>
和 <KeyRelease>
函数的问题,最终被引导到使用 Pynput 的监听器。
我唯一的问题是侦听器似乎没有输出或 return 一个我可以与字符串进行比较的字符。在下面的代码中,第 34-64 行是我的问题 - 具体来说,第 35、42、49 和 56 行。这些 if
语句 应该 使用密钥 return 从 Listener 编辑,如果它们相同,则在 TKinter 中移动我的角色。但是,无论我如何格式化 if
语句,它似乎都无法正常工作。
我原本以为 Listener 只是 returning 字符,但是用 print 语句测试似乎表明它是 returning 两边带有单引号的字符。
关于如何将 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()
通过提取 what Listener return 的字符串,找到所述 return 的 index[1]
并使用它移动来找到我的答案。还删除了我的移动函数中的 root.after(20, move_character, whatkey)
,因为它无限递归,这没有帮助。
我最近问了一个关于 TKinter 的 <KeyPress>
和 <KeyRelease>
函数的问题,最终被引导到使用 Pynput 的监听器。
我唯一的问题是侦听器似乎没有输出或 return 一个我可以与字符串进行比较的字符。在下面的代码中,第 34-64 行是我的问题 - 具体来说,第 35、42、49 和 56 行。这些 if
语句 应该 使用密钥 return 从 Listener 编辑,如果它们相同,则在 TKinter 中移动我的角色。但是,无论我如何格式化 if
语句,它似乎都无法正常工作。
我原本以为 Listener 只是 returning 字符,但是用 print 语句测试似乎表明它是 returning 两边带有单引号的字符。
关于如何将 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()
通过提取 what Listener return 的字符串,找到所述 return 的 index[1]
并使用它移动来找到我的答案。还删除了我的移动函数中的 root.after(20, move_character, whatkey)
,因为它无限递归,这没有帮助。