简体   繁体   English

Python Tkinter:在画布上单击时获取鼠标坐标作为全局变量

[英]Python Tkinter: Get mouse coordinates as global variables on click on canvas

I 've started experiencing Python very recently, moving from Matlab.我最近开始体验 Python,从 Matlab 开始。 My code uploads an image on canvas with tkinter.我的代码使用 tkinter 在画布上上传图像。 On mouse click on canvas image, I could get the mouse-click coordinates and display them in the console from the respective function (.bind).在画布图像上单击鼠标时,我可以获得鼠标单击坐标并从相应的函数 (.bind) 中将它们显示在控制台中。 My problem is that I want to use the mouse-click coordinates as global variables to my code (Cx and Cy in the example below), ie outside .bind events and functions.我的问题是我想将鼠标单击坐标用作我的代码(下面示例中的 Cx 和 Cy)的全局变量,即在 .bind 事件和函数之外。 I understand that tkinter doesn't pause the program to wait for a mouse click event, so, there, I guess, is the issue.我知道 tkinter 不会暂停程序以等待鼠标单击事件,所以,我想,这就是问题所在。 I 've noticed that there 've been several similar questions/answers in your site in the past, but still I can't resolve my problem.我注意到过去在您的网站上有几个类似的问题/答案,但我仍然无法解决我的问题。 Should I use other widget, if tkinter cannot support this?如果 tkinter 不支持,我应该使用其他小部件吗? Your help and opinion is deeply appreciated.非常感谢您的帮助和意见。 I apology if my question has been already addressed in your site;如果我的问题已经在您的网站上得到解决,我深表歉意; though I could not find the solution.虽然我找不到解决方案。 Here is an example of my code:这是我的代码示例:

import tkinter as tk 
import pandas as pd
import os
import sys

from tkinter.filedialog import askopenfilename
from PIL import Image, ImageTk

print("\033[H\033[J") # to clear the Spyder console
os.system('cls') #on Windows System to clear screen

root = tk.Tk()
w = tk.Canvas(root, width=1000, height=1000)
w.pack()
File = askopenfilename(parent=root, initialdir="./",title='Select an image')
original = Image.open(File)
img = ImageTk.PhotoImage(original)
w.create_image(0, 0, image=img, anchor="nw")

def getcoord(event):
    global Cx, Cy
    Cx = event.x
    Cy = event.y
    print('X = ', Cx, '   Y=  ', Cy)

def quit(event):                           
    sys.exit() 

w.bind('<Button-1>', getcoord)
w.bind('<Double-1>', quit)

root.mainloop()

It is not entirely clear what problem holds you up as your variables Cx and Cy are already available globally.由于您的变量CxCy已经在全球范围内可用,因此不完全清楚是什么问题阻碍了您。 To demonstrate it, here is a minimum example that prionts clicks coords in the console, and every 5 clicks calls another function that prints the coords without receiving a canvas event.为了演示它,这里是一个最小的例子,prionts 在控制台中点击坐标,每 5 次点击调用另一个打印坐标的函数,而不接收画布事件。

import tkinter as tk


def getcoord(event):
    global Cx, Cy
    Cx, Cy = event.x, event.y
    print('X = ', Cx, '   Y=  ', Cy)
    num_clicks.set(num_clicks.get() + 1)


def five_clicks(*args):
    if not num_clicks.get() % 5:
        print(f'after %5 clicks: {Cx=}, {Cy=}')


if __name__ == '__main__':

    Cx, Cy = 0, 0

    root = tk.Tk()
    num_clicks = tk.IntVar(0)
    num_clicks.set(0)
    num_clicks.trace_add('write', five_clicks)

    w = tk.Canvas(root, width=1000, height=1000)
    w.pack()

    w.bind('<Button-1>', getcoord)

    root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM