Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
event.keycode在python中不起作用_Python_Tkinter_Tkinter Canvas_Keycode - Fatal编程技术网

event.keycode在python中不起作用

event.keycode在python中不起作用,python,tkinter,tkinter-canvas,keycode,Python,Tkinter,Tkinter Canvas,Keycode,这里我只是设计一个简单的wandrer游戏,我想用Esc按钮退出游戏,用空格按钮攻击。我在我的代码中使用了每一个的mac密钥码,但它不起作用。这是我的main.py: m = Utils.matrix def on_key_press(event): #exit if event.keycode == 53: print(event.keycode) root.destroy() #battle opponent = False

这里我只是设计一个简单的wandrer游戏,我想用Esc按钮退出游戏,用空格按钮攻击。我在我的代码中使用了每一个的mac密钥码,但它不起作用。这是我的main.py:

 m = Utils.matrix
 def on_key_press(event):
    #exit
    if event.keycode == 53:
     print(event.keycode)
     root.destroy()

    #battle
    opponent = False
    for char in game.get_characters():
        if event.keycode == 49 and coords(hero) == coords(char):
            opponent = hero.battle(char)
    if opponent:
        game.remove_character(opponent)
    #initialise new game object and level up characters
    if game.get_char_length() < 1:
        new_level()
        game.create_monsters()
        game.level_up_chars(hero.level)
        hero.x = 0
        hero.y = 0
        hero.set_image(hero_down)

    canvas.delete("all")
    stats()
    draw_canvas()


canvas.bind("<KeyPress>", on_key_press)
canvas.pack()
m=Utils.matrix
def on_键按下(事件):
#出口
如果event.keycode==53:
打印(event.keycode)
root.destroy()
#战斗
对手=错误
用于游戏中的字符。获取字符()
如果event.keycode==49且coords(英雄)==coords(字符):
对手=英雄。战斗(字符)
如果对手:
游戏。移除角色(对手)
#初始化新游戏对象并升级角色
如果游戏。获取字符长度()<1:
新职等()
游戏。创造怪物()
游戏。升级角色(英雄。等级)
hero.x=0
hero.y=0
英雄。设置英雄形象(英雄放下)
canvas.delete(“全部”)
统计数据()
绘制画布()
canvas.bind(“,在按键上)
canvas.pack()
在这里,我只是尝试创建一个单独的函数,但它仍然不起作用,其他类似的函数,如keyup()、keydown()。。。除了“esc”和“空间”之外,它们工作得非常好

m=Utils.matrix
def坐标(字符):
x、 y=char.get_坐标()
x、 y=int(x/72),int(y/72)
返回x,y
def leftKey(事件):
如果m[coords(hero)[1][coords(hero)[0]-1]!=1和coords(英雄)[0]>0:#左
英雄。移动(x=-72)
英雄。设置英雄形象(英雄左)
def rightKey(事件):
如果m[coords(hero)[1][coords(hero)[0]+1]!=1和coords(英雄)[0]0:#向上
英雄。移动(y=-72)
英雄。设置图像(英雄设置)
def向下键(事件):

如果m[coords(hero)[1]+1][coords(hero)[0]!=1和coords(英雄)[1]你说“它不起作用”是什么意思?您是否尝试过打印(event.keycode)
以查看当您按下这些键时实际的键码是什么?为什么不直接绑定到escape键和space键,并调用每个键的专用函数?@AKX是的,我尝试过,但没有output@RoxanaSlj即使添加
print(event.keycode),也没有输出
您现在的位置
#退出
?我觉得这很难相信。@AKX我已经编辑了我的代码,并添加了print(),但控制台中并没有打印对象,调试器也并没有,我知道这听起来很愚蠢。我还不熟悉python和pycharm,所以我缺乏一些调试技巧
m = Utils.matrix
def coords(char):
    x, y = char.get_coordinates()
    x, y = int(x / 72), int(y / 72)
    return x, y
def leftKey(event):
  if  m[coords(hero)[1]][coords(hero)[0] - 1] != 1 and coords(hero)[0] > 0: #left
     hero.move(x=-72)
     hero.set_image(hero_left)

def rightKey(event):
    if  m[coords(hero)[1]][coords(hero)[0]+1] != 1 and coords(hero)[0]<=9:   #right
     hero.move(x=72)
     hero.set_image(hero_right)

def upKey(event):
    if  m[coords(hero)[1] - 1][coords(hero)[0]] != 1 and coords(hero)[1] > 0:  # up
     hero.move(y=-72)
     hero.set_image(hero_up)

def downKey(event):
    if  m[coords(hero)[1] + 1][coords(hero)[0]] != 1 and coords(hero)[1] <= 9:  # down
     hero.move(y=72)
     hero.set_image(hero_down)

def close(event):
    # canvas.withdraw()  # if you want to bring it back
    # sys.exit() # if you want to exit the entire thing
    root.destry()

root.bind('<Escape>', close)

root.bind('<Left>', leftKey)
root.bind('<Right>', rightKey)
root.bind('<Up>', upKey)
root.bind('<Down>', downKey)
# root.bind("<KeyPress>", close)




enter code here