Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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
Python 如何在tkinter中编程两个同时发生的按键事件,以使用按键事件字典对角移动画布项?_Python_Tkinter - Fatal编程技术网

Python 如何在tkinter中编程两个同时发生的按键事件,以使用按键事件字典对角移动画布项?

Python 如何在tkinter中编程两个同时发生的按键事件,以使用按键事件字典对角移动画布项?,python,tkinter,Python,Tkinter,下面是在画布上移动正方形的代码。 它捕获箭头键按下事件并上下左右移动方块。同时按下两个箭头(例如向上和向左)不会对角移动正方形。相反,它只在所需的两个方向中的一个方向上移动 如何修改此代码以实现正方形的平滑对角线移动。谢谢你抽出时间 from tkinter import * x = 10 y = 10 a = 100 b = 100 direction = None def move(): global x_vel global y_vel global direc

下面是在画布上移动正方形的代码。 它捕获箭头键按下事件并上下左右移动方块。同时按下两个箭头(例如向上和向左)不会对角移动正方形。相反,它只在所需的两个方向中的一个方向上移动

如何修改此代码以实现正方形的平滑对角线移动。谢谢你抽出时间

from tkinter import *

x = 10
y = 10
a = 100
b = 100
direction = None

def move():
    global x_vel
    global y_vel
    global direction
    if direction is not None:
        canvas1.move(rect, x_vel,y_vel)
    window.after(33,move)

def on_keypress(event):
    global direction
    global x_vel
    global y_vel
    direction, x_vel, y_vel = dir_vel[event.keysym]

def on_keyrelease(event):
    global direction
    direction = None

dir_vel = {
    "Left": ("left", -5, 0),
    "Right": ('right', 5, 0),
    "Down": ('down', 0, 5),
    "Up": ('up', 0, -5),}


window = Tk()
window.geometry("400x200")

move()

#canvas and drawing
canvas1=Canvas(window, height = 200, width = 400)
canvas1.grid(row=0, column=0, sticky=W)
coord = [x, y, a, b]
rect = canvas1.create_rectangle(*coord, outline="#fb0", fill="#fb0")

#capturing keyboard inputs and assigning to function
window.bind_all('<KeyPress>', on_keypress)
window.bind_all('<KeyRelease>', on_keyrelease)
从tkinter导入*
x=10
y=10
a=100
b=100
方向=无
def move():
全球x_水平
全球y_水平
全球方向
如果方向不是无:
画布1.移动(矩形、x层、y层)
窗后(33,移动)
def on_按键(事件):
全球方向
全球x_水平
全球y_水平
方向,x水平,y水平=方向水平[event.keysym]
def on_钥匙释放(事件):
全球方向
方向=无
目录级别={
“左”:(“左”、-5,0),
“右”:(“右”,5,0),
“向下”:(“向下”,0,5),
“向上”:(“向上”,0,-5),}
window=Tk()
窗口几何(“400x200”)
移动()
#画布和绘画
画布1=画布(窗口,高度=200,宽度=400)
canvas1.grid(行=0,列=0,粘滞=W)
坐标=[x,y,a,b]
rect=canvas1.创建矩形(*coord,outline=“#fb0”,fill=“#fb0”)
#捕获键盘输入并分配给函数
窗口。绑定所有(“”,在按键上)
窗口。绑定所有(“”,在按键释放时)

> p>我只是在玩同时按键,结果它们相隔10毫秒,所以如果你检查两个按钮在20毫秒之内被按下,你可以同时考虑它们。不过,我相信还有一个更优雅的解决方案。

我如何编程。。。事件 程序部分: Tkinter可以自己生成UI事件,而不会在UI的“前面”实际发生外部刺激。因此,“如何编写事件程序”部分使用以下方法完成:

self.event_generate( <eventNameId>, **args ) # fire STIMULUS without User-interaction
#                                            # triggers <eventNameId>
#                                            # **args allow to set <keyword>=<value>
#                                            #        pairs for Event-fields,
#                                            #        that are passed to anEventHANDLER
#                                            #        via an-<Event>-object ...
#                                            #        ref below ( not the system-assigned ones, sure )
为了检测此类事件,Tkinter配备了以下方法:

#                      |<<_aNamedEVENT_>>|<<______________________________aHANDLER>>|
#                      |or               |                                          |
#                      |<<_VirtualEVENT>>|                                          |
#                      |                 |                                          |
.bind(                  "<KeyPress-Left>", self.__doWidgetBoundTaskSpecificHANDLER  )
.bind_class( "Button",  "<KeyPress-Left>", self.__doClass_BoundTaskSpecificHANDLER  )
.bind_all(              "<KeyPress-Left>", self.__doApplicBoundTaskSpecificHANDLER  )

我相信这是的一个副本,它列在右侧作为一个相关问题。它类似,但这个问题专门询问另一方没有具体答案的同时按键事件。具体到使用字典同时按键事件
#                      |<<_aNamedEVENT_>>|<<______________________________aHANDLER>>|
#                      |or               |                                          |
#                      |<<_VirtualEVENT>>|                                          |
#                      |                 |                                          |
.bind(                  "<KeyPress-Left>", self.__doWidgetBoundTaskSpecificHANDLER  )
.bind_class( "Button",  "<KeyPress-Left>", self.__doClass_BoundTaskSpecificHANDLER  )
.bind_all(              "<KeyPress-Left>", self.__doApplicBoundTaskSpecificHANDLER  )
def on_keypress( event ):                          # keeping the Globals-style,
    global direction                               #         however shall be rather
    global x_vel                                   #         implemented in a Class-based
    global y_vel                                   #         manner

    direction     = dir_vel[event.keysym][0]       # ref. remark on more complex FSA
    x_vel        += dir_vel[event.keysym][1]
    y_vel        += dir_vel[event.keysym][2]

def on_keyrelease( event ):
    global direction
    global x_vel
    global y_vel
    x_vel        -= dir_vel[event.keysym][1]
    y_vel        -= dir_vel[event.keysym][2]
    if abs( x_vel * y_vel ) < 0.1:
        direction = None                          # ref. remark on more complex FSA