简体   繁体   English

如何等待鼠标点击?

[英]How to wait for a mouse click?

I want my code to start when I click left mouse button but my code starts right after I click on button.我希望我的代码在我单击鼠标左键时启动,但我的代码在我单击按钮后立即启动。 How to make my code wait for mouse input before continue?如何让我的代码在继续之前等待鼠标输入?

I already tried input() but it's not what I'm looking for.我已经尝试过input()但这不是我要找的。

from tkinter import *
import PIL.ImageGrab
from PIL import ImageGrab
import time
import numpy as np
import pyautogui
import win32api

def mouseposition():
    global xclick
    global yclick
    xclick, yclick = win32api.GetCursorPos()
    print(xclick, yclick)

def mouseclick():
    state_left = win32api.GetKeyState(0x01) # Left button down = 0 or 1. Button up = -127 or -128
    a = win32api.GetKeyState(0x01)
    if a != state_left:
        mouseposition() # Button state changed
        state_left = a
        print("1")
    else:
        mouseposition()
        print("2")

def something():
    window.update()
    mouseclick()

window = Tk()
window.geometry("700x500")
window.title("Testing")

b = Button(window, text="OK", command=something)
b.grid(row=0, column=2, sticky=W)

window.update()
window.mainloop()

Function something starts function mouseclick right after I click on OK button and I get the xclick, yclick of the OK button.在我单击“确定”按钮后,某些功能立即启动功能mouseclick单击,我得到了“确定”按钮的xclick, yclick I want function mouseclick to wait until I aim at something on screen and click on left mouse button and THEN give me xclick, yclick of place that I click on.我希望功能mouseclick等到我瞄准屏幕上的某个东西并单击鼠标左键然后给我 xclick,我单击的地方的xclick, yclick

Why do you use the win32api to get the mousebutton press and location?为什么要使用win32api来获取鼠标按钮的按下和位置? Tkinter can do this just fine. Tkinter 可以很好地做到这一点。 You can use the OK button to activate a new left-mousebutton bind and let that bind disable itself when it has been called.您可以使用 OK 按钮激活新的鼠标左键绑定,并让该绑定在被调用时自行禁用。 You can even change the cursor to indicate that the program is in a state where you expect the user to click on a location:您甚至可以更改光标以指示程序处于您希望用户单击某个位置的状态:

from tkinter import *


def enable_mouseposition():
    window.bind("<Button-1>", get_mouseposition)
    window.config(cursor="crosshair")


def get_mouseposition(event):
    print(event.x, event.y)
    window.unbind("<Button-1>")
    window.config(cursor="arrow")

window = Tk()
window.geometry("700x500")
window.title("Testing")

b = Button(window, text="OK", command=enable_mouseposition)
b.grid(row=0, column=2, sticky=W)


window.mainloop()

I now understand you want to be able to get the click everywhere on the screen, not just in the tkinter window.我现在明白你希望能够在屏幕上的任何地方获得点击,而不仅仅是在 tkinter 窗口中。 In that case you will need something besides tkinter, like win32api .在那种情况下,除了 tkinter 之外,您还需要一些东西,比如win32api Also, because you can not generate a tkinter event by clicking outside the window, you would need a loop that checks the button state repeatedly until the button is clicked.此外,由于您不能通过在窗口外单击来生成 tkinter 事件,因此您需要一个循环来重复检查按钮状态,直到单击按钮为止。 You can make a loop in tkinter without blocking the mainloop using after :您可以在 tkinter 中创建一个循环,而不会使用after阻塞主循环:

from tkinter import *
import win32api


def enable_mouseposition():
    window.after(10, get_mouseposition)


def get_mouseposition():
    state_left = win32api.GetKeyState(0x01)
    if state_left == -127 or state_left == -128:
        xclick, yclick = win32api.GetCursorPos()
        print(xclick, yclick)
    else:
        window.after(10, get_mouseposition)

window = Tk()
window.geometry("700x500")
window.title("Testing")

b = Button(window, text="OK", command=enable_mouseposition)
b.grid(row=0, column=2, sticky=W)

window.mainloop()

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

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