1

我被困住了。这是我的同事通过单击刷新按钮刷新网页的帮助应用程序。到目前为止不起作用的是抓取 x,y 坐标 (getx,gety) 并且在按下 F5 后 GUI 锁定。我认为问题是当函数 on_activate 被调用时,pyautogui 中的 x,y 在函数之外不可用。也许?我不得不添加一个函数 get_position 因为监听器导致 GUI 永远不会填充。

我仍在学习python,因此不胜感激。我觉得我是如此接近,但错过了一些东西。

"""
Created on Mon Dec 16 12:13:06 2019
Auto clicker for page refresh. Looks for hotkey to grab the mouse location, F5. The button GetPos must be pressed before F5 is available. Then Start Clicking can be pressed and the mouse should click on the x,y coordinates once per delay setting.
The Pause button will pause clicking.
"""
from pynput import keyboard
import pynput
import PySimpleGUI as sg
import pyautogui
import time
i = 1
getx=None
gety=None
delay =None

#-------------Functions-------------------------------------------------------------------------------------------------

def stop_clicking(): # for pause button
    pass

def on_activate(): # get click location
    print('Global hotkey activated!')
    getx, gety = pyautogui.position()
    positionStr = 'X: ' + str(getx).rjust(4) + ' Y: ' + str(gety).rjust(4)
    print(positionStr)
    end_listener()
    return(getx,gety)

def end_listener(): # end keyboard listener, but does not work
    pynput.keyboard.Listener.stop
    print('end listener')

def ClickMouse(): # click mouse at location loop
    while (i < 20):
        time.sleep(delay)
        pyautogui.click(getx, gety)
        print('clicked'+ getx)

def get_position(): # keyboard listener for hotkey
    hotkey = keyboard.HotKey(
        keyboard.HotKey.parse('<f5>'),
        on_activate)
    with keyboard.Listener(
         on_press=hotkey.press,
        on_release=hotkey.release) as l:
        l.join()

# ----------------  Create PYSimpleGUI Form-----------------------------------------------------------------------------
sg.ChangeLookAndFeel('LightBlue')
sg.SetOptions(element_padding=(0, 0))

layout = [[sg.Text('Press F5 to get the mouse position. ')],
          #[sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='text')],
          [sg.Text('X: ' + str(getx).ljust(4) + ' Y: ' + str(gety).ljust(4), size=(10, 2), font=('Helvetica', 10), justification='center', key='text2')],
          [sg.Text('Delay in Seconds ', font=('Helvetica', 10), justification='center', key='text'),sg.InputText('', size=(3, 1), font=('Helvetica'), key='delay'), sg.Button('Submit',key='submit')],
          [sg.Text('', size=(8, 1), font=('Helvetica', 10), justification='center', key='text3')],
          [sg.ReadButton('Pause', key='pause', button_color=('white', '#001480')),
           sg.ReadButton('Start Clicking', button_color=('white', '#007339'), key='Start'),
           sg.ReadButton('GetPos', button_color=('white', '#007339'), key='getPos'),
           sg.Exit(button_color=('white', 'firebrick4'), key='Exit')]]

window = sg.Window('Refresher', layout, no_titlebar=True, auto_size_buttons=False, keep_on_top=True,
                   grab_anywhere=True)

getx,gety = on_activate() # works to get a getx and gety value but calls function to soon, duh
print(getx) # see if I get a value for getx (debuging)

keys_entered = 'X: ' + str(getx).ljust(4) + ' Y: ' + str(gety).ljust(4) #variable for window refresh below

while True:  # Event Loop
    event, values = window.read()
    if event is None:  # X (close) on the window
        break
    elif event == 'submit':
        delay = values['delay']
    elif event == 'Pause':
        stop_clicking()
    elif event == 'Start':
        ClickMouse()
    elif event == 'Exit':
        exit
    elif event =='getPos':
        get_position()
    window['text2'].update(keys_entered)  # change the window to reflect current key string, so far does not work

window.close()

使用 wind32api 替换 pyautogui 的新代码。

import PySimpleGUI as sg
import time
import win32api
import win32con
import sys

i = 1
x=None
y=None
delay =None
paused = False

#-------------Functions----------------------------------------------------------

def stop_clicking(): # for pause button
    pass

def on_activate(): # get click location
    print('Global hotkey activated!')
    x, y = win32api.GetCursorPos()
    win32api.SetCursorPos((x, y))
    positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
    print(positionStr)
    time.sleep(2)

def where_is():
    print (x)
    print (y)

def ClickMouse(): # click mouse at location loop
    while True:
        win32api.SetCursorPos((x, y))
        time.sleep(.05)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
        time.sleep(0.05)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
        print('clicked'+ x)
        time.sleep(4)

# ----------------  Create PYSimpleGUI Form--------------------------------------
sg.ChangeLookAndFeel('LightBlue')
sg.SetOptions(element_padding=(0, 0))

layout = [[sg.Text('After pressing Get Pos, Press F5 to get the mouse position. ')],
          #[sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='text')],
          [sg.Text('X: ' + str(x).ljust(4) + ' Y: ' + str(y).ljust(4), size=(20, 1), font=('Helvetica', 10), justification='center', key='text2')],
          [sg.Text('Delay in Seconds ', font=('Helvetica', 10), justification='center', key='text'),sg.InputText('', size=(3, 1), font=('Helvetica'), pad=(2,0) , key='delay'), sg.Button('Submit',key='submit')],
          [sg.Text('', size=(8, 1), font=('Helvetica', 10), justification='center', key='text3')],
          [sg.ReadButton('Pause', key='pause', button_color=('white', '#001480')),
           sg.ReadButton('Start Clicking', button_color=('white', '#007339'), key='Start'),
           sg.ReadButton('Get Pos', key='getpos', button_color=('orange', 'black')),
           sg.Exit(button_color=('white', 'firebrick4'), key='Exit')]]

window = sg.Window('Refresher', layout, no_titlebar=True, auto_size_buttons=False, keep_on_top=True,
                   grab_anywhere=True)

keys_entered = 'X: ' + str(x).ljust(4) + ' Y: ' + str(y).ljust(4) #variable for window refresh below

while True:  # Event Loop
    event, values = window.read()
    if event is None:  # X (close) on the window
        break
    elif event == 'submit':
        delay = values['delay']
    elif event == 'Pause':
        paused =True
        element = window('start')
        element.update(text='Start Clicking')
        stop_clicking()
    elif event == 'Start':
        paused = False
        ClickMouse()
    elif event == 'Exit':
        sys.exit()


if win32api.GetAsyncKeyState(0x74):
             print('Global hotkey activated!')
             x, y = win32api.GetCursorPos()
             win32api.SetCursorPos((x, y))
             positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
             print(positionStr)
             time.sleep(2)

window['text2'].update(keys_entered)  # change the window to reflect current key string, so far does not work

window.close()
4

0 回答 0