【问题标题】:How to catch two key events in Tkinter如何在 Tkinter 中捕获两个关键事件
【发布时间】:2017-04-11 04:02:17
【问题描述】:

所以我在 tkinter 中制作小型发光曲棍球游戏,我面对墙壁。编译器只捕获一个键事件,如果第二个用户按下键,第一个用户移动将停止。你们知道如何解决这个问题吗?

代码如下:

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()

【问题讨论】:

    标签: python python-3.x tkinter tkinter-canvas


    【解决方案1】:

    在其他模块中——比如PyGame——你使用像w_pressed = True/Falseup_pressed = True/False这样的变量,当你按下或释放键时你会改变它们。接下来创建mainloop,它会检查此变量以移动对象。因为tkinter 已经有了mainloop,所以你可以使用after() 定期执行自己的函数,该函数将检查w_pressed/up_pressed 并移动对象。

    简单(工作)示例:

    它检查wup 并为这两个键显示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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 2013-06-21
      • 2011-11-22
      相关资源
      最近更新 更多