简体   繁体   中英

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.

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. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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