如何退出Python程序或通过键绑定或宏循环?键盘中断不起作用 [英] How to exit a Python program or loop via keybind or macro? Keyboardinterrupt not working

查看:130
本文介绍了如何退出Python程序或通过键绑定或宏循环?键盘中断不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试完成一个简单的GUI自动化程序,该程序仅打开一个网页,然后每0.2秒单击该页面上的特定位置,直到我告诉它停止为止.我希望我的代码能够运行并无限循环运行,直到我指定的按键绑定打破了循环(或整个程序).我从经典的KeyboardInterrupt开始,它使CTRL + C可以退出程序.这是我认为我的最终代码将是这样的:

I am trying to complete a simple GUI automation program that merely opens a web page and then clicks on a specific spot on the page every 0.2 seconds until I tell it to stop. I want my code to run and have its loop run infinitely until a keybind I specify breaks the loop (or entire program). I started out with the classic, KeyboardInterrupt, which enables CTRL+C to exit a program. Here is what I thought my final code would look like:

import webbrowser, pyautogui, time
webbrowser.open('https://example.com/')
print('Press Ctrl-C to quit.')
time.sleep(5)
#pyautogui.moveTo(1061, 881)
try:
    while True:
            time.sleep(0.2)
            pyautogui.click(1061,881)
except KeyboardInterrupt:
    print('\nDone.')

关于代码的所有工作均有效,但单击环开始后我无法退出它.无论出于何种原因,对于此脚本,键盘中断和使用CTRL-C退出都不起作用.

Everything about the code works, except the fact that I can't exit it once the clicking loop starts. Keyboard interrupt and using CTRL-C to exit do not work at all for this script, for whatever reason.

我只希望能够按退出"键(或任何其他键)退出循环(或整个程序),这只是使循环退出和停止的任何方式.现在它可以无限运行,但是我希望一个简单的keybind宏能够停止/破坏它.

I merely want to be able to press "escape" (or any other key) to exit the loop (or the program altogether) - just any way to make the loop exit and stop. Right now it runs ad infinitum, but I want a simple keybind macro to be able to stop/break it.

我尝试使用getch键将转义键进行绑定以引起中断,但无济于事:

I've tried using getch to keybind the escape key to cause a break, but to no avail:

import webbrowser, pyautogui, time, msvcrt
webbrowser.open('https://example.com')
print('Press Ctrl-C to quit.')
time.sleep(5)
#pyautogui.moveTo(1061, 881)
try:
    while True:
            time.sleep(0.2)
            pyautogui.click(1061,881)
            if msvcrt.kbhit():
                key = ord(readch())
                if key == 27:
                    break

我很惊讶在Python中很难做到这一点.我已经在Stackoverflow上检查了很多类似的问题,但是答案不尽人意,但是不幸的是,没有一个问题可以解决我的问题.我已经能够轻松地用更简单的编码语言(例如AuotHotKeys)来做这样的事情.我觉得我在解决方案中跳舞.任何和所有帮助将不胜感激!预先感谢.

I'm surprised it's been so hard to do this in Python. I've checked out a lot of similar problems across Stackoverflow, but with unsatisfactory answers, and none that solve my problem, unfortunately. I've been able to do things like this in simpler coding languages like AuotHotKeys with ease. I feel like I'm dancing around the solution. Any and all help would be wonderfully appreciated! Thanks in advance.

推荐答案

如果我理解正确,则希望能够通过按键盘上的键来停止程序.

If I understood correctly, you want to be able to stop your program by pressing a key on your keyboard.

要使您创建一个线程,如果您按下相应的键,它将在后台签入.

To make you create a thread that will check in background if you press the key in question.

一个小例子:

import threading, time
from msvcrt import getch

key = "lol"

def thread1():
    global key
    lock = threading.Lock()
    while True:
        with lock:
            key = getch()

threading.Thread(target = thread1).start() # start the background task

while True:
    time.sleep(1)
    if key == "the key choosen":
        # break the loop or quit your program

希望它的帮助.

这篇关于如何退出Python程序或通过键绑定或宏循环?键盘中断不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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