Python 如何捕捉Tkinter中的两个关键事件

Python 如何捕捉Tkinter中的两个关键事件,python,python-3.x,tkinter,tkinter-canvas,Python,Python 3.x,Tkinter,Tkinter Canvas,所以我在tkinter中制作了一个小的glow hokey游戏,我面对着墙。编译器只捕捉到一键事件,如果第二个用户按下键,第一个用户的移动将停止。你们知道怎么解决这个问题吗 以下是代码: from tkinter import* w=600 h=300 padis_sigane=10 padis_sigrdze=75 padis_sichqare=5 root=Tk() root.geometry("{}x{}".format(w,h)) root.resizable(False,False)

所以我在tkinter中制作了一个小的glow hokey游戏,我面对着墙。编译器只捕捉到一键事件,如果第二个用户按下键,第一个用户的移动将停止。你们知道怎么解决这个问题吗

以下是代码:

from tkinter import*
w=600
h=300
padis_sigane=10
padis_sigrdze=75
padis_sichqare=5
root=Tk()
root.geometry("{}x{}".format(w,h))
root.resizable(False,False)
c=Canvas(root,width=w,height=h,bg="green")
c.create_line(w//2,0,w//2,h,width=10,fill="white")
c.create_line(padis_sigane,0,padis_sigane,h,width=2,fill="white")
c.create_line(w-padis_sigane,0,w-padis_sigane,h,width=2,fill="white")
c.create_oval(w//2-w//30,h//2-w//30,w//2+w//30,h//2+w//30,fill="white",outline="white")
class chogani:
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.pad=c.create_rectangle(self.x,self.y,self.x+padis_sigane,self.y+padis_sigrdze,fill="lightblue",outline="white")
    def shxuili(self):

        if c.coords(self.pad)[3]>=h:
            c.coords(self.pad,self.x,h-padis_sigrdze,self.x+padis_sigane,h)

        elif c.coords(self.pad)[1]<=0:
            c.coords(self.pad,self.x,0,self.x+padis_sigane,padis_sigrdze)


x=0;y=0 #Momavalshi
pad1=chogani(0,1)
pad2=chogani(w-padis_sigane,1)
def K(event):
    pad1.shxuili()
    pad2.shxuili()
    if event.keysym=='w':
        c.move(pad1.pad,0,-padis_sichqare)
    elif event.keysym=='s':
        c.move(pad1.pad,0,padis_sichqare)
    elif event.keysym=='Up':
        c.move(pad2.pad,0,-padis_sichqare)
    elif event.keysym=='Down':
        c.move(pad2.pad,0,padis_sichqare)
def R(event):
    print("shen aushvi ", event.char)
root.bind("<KeyPress>",K)
root.bind("<KeyRelease>",R)
root.focus_set()
c.pack()
root.mainloop()
从tkinter导入*
w=600
h=300
padis_sigane=10
padis_sigrdze=75
padis_sichqare=5
root=Tk()
几何体(“{}x{}”.format(w,h))
根目录。可调整大小(False,False)
c=画布(根,宽=w,高=h,bg=“绿色”)
c、 创建线(w//2,0,w//2,h,宽度=10,fill=“白色”)
c、 创建线(padis_sigane,0,padis_sigane,h,宽度=2,fill=“白色”)
c、 创建线(w-padis-u-sigane,0,w-padis-u-sigane,h,宽度=2,fill=“白色”)
c、 创建椭圆(w//2-w//30,h//2-w//30,w//2+w//30,h//2+w//30,fill=“white”,outline=“white”)
乔加尼类:
定义初始化(self,x,y):
self.x=x
self.y=y
self.pad=c.create_矩形(self.x,self.y,self.x+padis_sigane,self.y+padis_sigrdze,fill=“lightblue”,outline=“white”)
def shxuili(自我):
如果c.coords(self.pad)[3]>=h:
c、 坐标(self.pad,self.x,h-padis_-sigrdze,self.x+padis_-sigane,h)

elif c.coords(self.pad)[1]在其他模块中-如
PyGame
-您可以使用
w_pressed=True/False
up_pressed=True/False
等变量,在按下或松开按键时可以更改这些变量。接下来创建
mainloop
,它检查这些变量以移动对象。因为
tkinter
已经有了
mainloop
,所以您可以使用
after()
定期执行自己的函数,该函数将检查
w\u pressed
/
up\u pressed
并移动对象

简单(工作)示例:

它检查
w
up
并显示两个键的
True/False

import tkinter as tk

# --- functions ---

def pressed(event):
    global w_pressed
    global up_pressed

    if event.keysym == 'w':
        w_pressed = True
    elif event.keysym == 'Up':
        up_pressed = True

def released(event):
    global w_pressed
    global up_pressed

    if event.keysym == 'w':
        w_pressed = False
    elif event.keysym == 'Up':
        up_pressed = False

def game_loop():

    # use keys 
    print(w_pressed, up_pressed)

    # run again after 500ms
    root.after(500, game_loop)

# --- data ---

w_pressed = False
up_pressed = False

# --- main ---

root = tk.Tk()

root.bind("<KeyPress>", pressed)
root.bind("<KeyRelease>", released)

# start own loop
game_loop()

root.mainloop()
将tkinter作为tk导入
#---功能---
def按下(事件):
全球w_出版社
全球上升压力
如果event.keysym==“w”:
w_pressed=True
elif event.keysym==“向上”:
向上按=真
def释放(事件):
全球w_出版社
全球上升压力
如果event.keysym==“w”:
w_pressed=False
elif event.keysym==“向上”:
向上按=错误
def game_loop():
#使用钥匙
打印(w_按下,up_按下)
#500毫秒后再次运行
root.after(500,游戏循环)
#---数据---
w_pressed=False
向上按=错误
#---梅因---
root=tk.tk()
root.bind(“,按下)
root.bind(“,已发布)
#启动自己的循环
博弈(循环)
root.mainloop()