我被困住了。这是我的同事通过单击刷新按钮来刷新网页的帮助程序。 到目前为止,行不通的是获取x,y坐标(getx,gety),并且在按F5键之后GUI锁定。我认为问题是当函数pyautogui中的函数on_activate被称为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()