Tkinter 从函数中获取按键事件 [英] Tkinter getting key pressed event from a function

查看:26
本文介绍了Tkinter 从函数中获取按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,

如果我按左箭头键",它只会打印move player to left但我需要一个功能,在这个功能中,按下给定的箭头键可以让玩家朝给定的方向移动.

有没有办法在我的 move_dir 函数中检测按键事件
PS:对python相当陌生

If I press 'Left Arrow Key', it only prints move player to left But I need a functionality in which pressing the given arrow key moves the player in the given direction.

Is there a way in which I can detect key press event in my move_dir function
PS: fairly new to python

import Tkinter as tk

move = 1
pos = -1


def move_dir():
    global move
    global pos
    while move ==1:
        if pos == 0:
            print 'move player to left'

        elif pos == 1:
            print 'move player to right'

        elif pos == -1:
            print 'stop moving!'

def kr(event):
    global move
    global pos
    global root
    if event.keysym == 'Right':
        move = 1
        pos = 0
        move_dir()
        print 'right ended'
    elif event.keysym == 'Left':
        move = 1
        pos = 1
        move_dir()
        print 'left ended'
    elif event.keysym == 'Space':
        move = 0
        move_dir()
    elif event.keysym == 'Escape':
        root.destroy()

root = tk.Tk()
print( "Press arrow key (Escape key to exit):" )
root.bind_all('<KeyRelease>', kr)
root.mainloop()

推荐答案

EDIT 4

您有一个想要与 Tkinter 主循环结合的 while 循环.在这种情况下,您希望在按下键时移动并在松开键时停止移动.下面的代码允许您执行此操作:

You have a while loop that you want to combine with the Tkinter mainloop. In this case you want to move when the key is pressed and stop moving when a key is released. The code below allows you to do this:

import Tkinter as tk
from guiLoop import guiLoop # https://gist.github.com/niccokunzmann/8673951#file-guiloop-py

direction = 0
pos = 0 # the position should increase and decrease depending on left and right
# I assume pos can be ... -3 -2 -1 0 1 2 3 ...

@guiLoop
def move_dir():
    global pos
    while True: # edit 1: now looping always
        print 'moving', direction 
        pos = pos + direction
        yield 0.5 # move once every 0.5 seconds

def kp(event):
    global direction # edit 2
    if event.keysym == 'Right':
        direction  = 1 # right is positive
    elif event.keysym == 'Left':
        direction = -1
    elif event.keysym == 'Space':
        direction = 0 # 0 is do not move
    elif event.keysym == 'Escape':
        root.destroy()

def kr(event):
    global direction
    direction = 0

root = tk.Tk()
print( "Press arrow key (Escape key to exit):" )
root.bind_all('<KeyPress>', kp)
root.bind_all('<KeyRelease>', kr)
move_dir(root)
root.mainloop()

要了解这是如何实现的,您可以阅读源代码或阅读 Bryan Oakley 的第二个答案.

To see how this is implemented you can read the source code or read the second answer by Bryan Oakley.

编辑 3

无法直接在 move_dir 函数中检测按键.您可以在 move_dir 函数中使用 root.update() 使 kr, kp 成为可能当 root.update 被调用时执行.root.update() 还会重新绘制窗口,以便用户可以看到更改.

There is no way to detect a keypress in the move_dir function directly. You can use root.update() in your move_dir function to make it possible for kr, kp to be executed when root.update is called. root.update() alse repaints the windows so that changes can be seen by the user.

root.mainloop() 可以看做 while True: root.update()

这篇关于Tkinter 从函数中获取按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆