如何等待鼠标单击? [英] How to wait for a mouse click?

查看:72
本文介绍了如何等待鼠标单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击鼠标左键时,我希望我的代码开始,但是单击按钮后,我的代码立即开始.如何让我的代码在继续输入之前先等待鼠标输入?

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?

我已经尝试过 input(),但这不是我想要的.

I already tried input() but it's not what I'm looking for.

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

功能,当我单击 OK 按钮后,我便得到了 OK <的 xclick,yclick 按钮.我希望函数 mouseclick 一直等到我瞄准屏幕上的某个东西并单击鼠标左键,然后再给我 xclick,yclick 我所单击的位置.

Function something starts function mouseclick right after I click on OK button and I get the xclick, yclick of the OK button. 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.

推荐答案

为什么使用 win32api 来获取鼠标按键的按下和位置?Tkinter可以做到这一点.您可以使用确定"按钮激活一个新的鼠标左键绑定,并在调用该绑定时使其自身禁用.您甚至可以更改光标以表明该程序处于您希望用户单击某个位置的状态:

Why do you use the win32api to get the mousebutton press and location? Tkinter can do this just fine. You can use the OK button to activate a new left-mousebutton bind and let that bind disable itself when it has been called. 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()


我现在了解到您希望能够在屏幕上的所有位置获得点击,而不仅仅是在tkinter窗口中.在这种情况下,除了tkinter之外,您还需要其他东西,例如 win32api .另外,由于无法通过在窗口外部单击来生成tkinter事件,因此需要一个循环来反复检查按钮状态,直到单击按钮为止.您可以在tkinter中创建一个循环而无需使用 after 之后的主循环:


I now understand you want to be able to get the click everywhere on the screen, not just in the tkinter window. In that case you will need something besides tkinter, like 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. You can make a loop in tkinter without blocking the mainloop using 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()

这篇关于如何等待鼠标单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆