Python 3.x tkinter和鼠标位置

Python 3.x tkinter和鼠标位置,python-3.x,tkinter,mouseevent,Python 3.x,Tkinter,Mouseevent,我有一个这样的函数 (x_co_ms是一家全球性公司) 我这样称呼她,我的问题是,即使在我可以检索鼠标点击器的坐标之前,条件就已经执行了 如果你想要完整的代码并在这里尝试,只需点击Jouer链接 def创建图形板(): 窗板=顶层(主窗) 窗口板标题(“Puissance4 Plateaux de Jeux”) 窗板几何形状(“720x480”) 窗板绑定(“”,鼠标协作) 画布=画布(窗板,bg=“蓝色”,宽度=720,高度=480) 画布。创建_矩形(0,0,720,480,fill=“蓝

我有一个这样的函数 (x_co_ms是一家全球性公司)

我这样称呼她,我的问题是,即使在我可以检索鼠标点击器的坐标之前,条件就已经执行了

如果你想要完整的代码并在这里尝试,只需点击Jouer链接

def创建图形板():
窗板=顶层(主窗)
窗口板标题(“Puissance4 Plateaux de Jeux”)
窗板几何形状(“720x480”)
窗板绑定(“”,鼠标协作)
画布=画布(窗板,bg=“蓝色”,宽度=720,高度=480)
画布。创建_矩形(0,0,720,480,fill=“蓝色”)
acc_x=10
对于范围(7)内的x:
附件y=10
对于范围(6)内的y:
画布。创建椭圆(acc_x,acc_y,acc_x+70,acc_y+70,fill=“白色”)
acc_y+=75
acc_x+=75
canvas.pack()
#窗口\板。绑定(“”,鼠标\协调)用于始终更新co
如果x_co_ms在范围(10,80)内:
rond=canvas.create_oval(10,10,80,80,fill='red')
canvas.pack()
对于范围(19)中的i:
画布移动(rond,0,20)
睡眠时间(0.1)
canvas.update()
其他:
打印(“再见”)

感谢您的帮助祝您愉快。

我们不需要完整的代码。相反,创建一个专门针对问题的函数。随着鼠标的移动,函数将被执行,这有什么问题。执行
if
语句,因为它不在函数范围内,并且在第一次调用
create\u graphical\u board()
时运行。试着把它放在
鼠标\u coordonates()
内。对于
x_co_ms
,要在功能之外进行更新,你必须说
global x_co_ms
Cool Cloud我不能在函数中使用if,因为我需要在之后创建一个画布,我不能给我的函数一个画布或窗口,所以我没有选择,但是如果你有一个解决方案给函数一个画布,请告诉我Bryan Oakley,以及你可以启动代码并复制的链接我的错误,所以我认为这就足够了
def mouse_coordonates(event):
    x_co_ms = event.x
def creat_graphical_board():
    window_board = Toplevel(main_window)
    window_board.title("Puissance4 Plateaux de Jeux")
    window_board.geometry("720x480")
    window_board.bind('<Button-1>', mouse_coordonates)
    canvas = Canvas(window_board, bg="blue", width=720, height=480)
    canvas.create_rectangle(0, 0, 720, 480, fill="blue")
    acc_x = 10
    for x in range(7):
        acc_y = 10
        for y in range(6):
            canvas.create_oval(acc_x, acc_y, acc_x+70,acc_y + 70, fill="white")
            acc_y += 75
        acc_x += 75
    canvas.pack()
    # window_board.bind('<Motion>',mouse_coordonates) for always update co

    if x_co_ms in range(10, 80):
        rond = canvas.create_oval(10,10,80,80,fill='red')
        canvas.pack()
        for i in range(19):
            canvas.move(rond,0,20)
            time.sleep(0.1)
            canvas.update()
    else:
        print("bye")