如何使用 pyautogui 添加开和关按钮?
How to add On and Off buttons with pyautogui?
我在 python 中制作了一个简单的自动答题器。脚本每三秒钟点击鼠标所在的位置。我将如何添加“开和关”键?我想这是一个简单的 if
/else
语句,但我不知道怎么写。
截至 9 月 15 日星期三 12:10,我没有一个有效的答案。
import pyautogui
import time
def Auto_Click():
width, height = pyautogui.position()
pyautogui.click(width, height)
time.sleep(3)
while True:
Auto_Click()
我建议无限期地聆听特定按键的声音以打开和关闭点击。由于点击也有无限循环,因此您将需要多线程(同时执行点击和监听按键)。
备注
- 默认情况下,自动答题器在启动时立即关闭(以避免在 运行 之后立即在屏幕上不需要的位置点击)。将鼠标指向所需位置后按
SHIFT
切换。
- 按
ESC
退出程序。
- 我使用
SHIFT
和 ESC
键进行切换,这样按键操作就不会像字符键那样出现在下一个提示中。
- 如果你真的需要使用pyautogui,请使用下面的代码。 Here 是使用 pynput 处理鼠标和键盘的解决方案。 (我的代码基本上是使用键盘模块和 pyautogui 代替的修改版本)
import time
import keyboard
import pyautogui
import threading
INTERVAL = 0.5 # Time interval between consecutive clicks
DELAY = 0.5 # Time delay between consecutive program cycles [after the clicks are turned off]
TOGGLE_KEY = 'shift' # Key to toggle the clicking
EXIT_KEY = 'esc' # Key to stop and exit from the program
class AutoClicker(threading.Thread):
def __init__(self, interval, delay):
super(AutoClicker, self).__init__()
self.interval = interval
self.delay = delay
self.running = False
self.program_running = True
def start_clicking(self):
self.running = True
def stop_clicking(self):
self.running = False
def exit(self):
self.stop_clicking()
self.program_running = False
def toggle_clicking(self):
if self.running:
self.stop_clicking()
else:
self.start_clicking()
def click(self):
width, height = pyautogui.position()
pyautogui.click(width, height)
# This function is invoked when the thread starts.
def run(self):
while self.program_running:
while self.running:
self.click()
time.sleep(self.interval)
time.sleep(self.delay)
if __name__ == '__main__':
# Run indefinite loop of clicking on seperate thread
auto_clicker_thread = AutoClicker(INTERVAL, DELAY)
auto_clicker_thread.start() # Invokes run() function of the thread
# So that we can listen for key presses on the main thread
keyboard.add_hotkey(TOGGLE_KEY, lambda: auto_clicker_thread.toggle_clicking())
keyboard.add_hotkey(EXIT_KEY, lambda: auto_clicker_thread.exit())
我在 python 中制作了一个简单的自动答题器。脚本每三秒钟点击鼠标所在的位置。我将如何添加“开和关”键?我想这是一个简单的 if
/else
语句,但我不知道怎么写。
截至 9 月 15 日星期三 12:10,我没有一个有效的答案。
import pyautogui
import time
def Auto_Click():
width, height = pyautogui.position()
pyautogui.click(width, height)
time.sleep(3)
while True:
Auto_Click()
我建议无限期地聆听特定按键的声音以打开和关闭点击。由于点击也有无限循环,因此您将需要多线程(同时执行点击和监听按键)。
备注
- 默认情况下,自动答题器在启动时立即关闭(以避免在 运行 之后立即在屏幕上不需要的位置点击)。将鼠标指向所需位置后按
SHIFT
切换。 - 按
ESC
退出程序。 - 我使用
SHIFT
和ESC
键进行切换,这样按键操作就不会像字符键那样出现在下一个提示中。 - 如果你真的需要使用pyautogui,请使用下面的代码。 Here 是使用 pynput 处理鼠标和键盘的解决方案。 (我的代码基本上是使用键盘模块和 pyautogui 代替的修改版本)
import time
import keyboard
import pyautogui
import threading
INTERVAL = 0.5 # Time interval between consecutive clicks
DELAY = 0.5 # Time delay between consecutive program cycles [after the clicks are turned off]
TOGGLE_KEY = 'shift' # Key to toggle the clicking
EXIT_KEY = 'esc' # Key to stop and exit from the program
class AutoClicker(threading.Thread):
def __init__(self, interval, delay):
super(AutoClicker, self).__init__()
self.interval = interval
self.delay = delay
self.running = False
self.program_running = True
def start_clicking(self):
self.running = True
def stop_clicking(self):
self.running = False
def exit(self):
self.stop_clicking()
self.program_running = False
def toggle_clicking(self):
if self.running:
self.stop_clicking()
else:
self.start_clicking()
def click(self):
width, height = pyautogui.position()
pyautogui.click(width, height)
# This function is invoked when the thread starts.
def run(self):
while self.program_running:
while self.running:
self.click()
time.sleep(self.interval)
time.sleep(self.delay)
if __name__ == '__main__':
# Run indefinite loop of clicking on seperate thread
auto_clicker_thread = AutoClicker(INTERVAL, DELAY)
auto_clicker_thread.start() # Invokes run() function of the thread
# So that we can listen for key presses on the main thread
keyboard.add_hotkey(TOGGLE_KEY, lambda: auto_clicker_thread.toggle_clicking())
keyboard.add_hotkey(EXIT_KEY, lambda: auto_clicker_thread.exit())