[英]Python: Start and stop thread using keyboard presses
我正在研究 Python 3.8,我正在尝试使用键盘快捷键打开和关闭线程。
这是我的线程 class:
import keyboard
from threading import Thread
import time
class PrintHi(Thread):
def __init__(self):
Thread.__init__(self)
self.active = False
def run(self):
while True:
if self.active:
print("Hi,", time.time())
time.sleep(1)
它似乎按预期工作,我可以启动线程,然后根据我是要启用还是禁用,将“thread.active”更改为 True 或 False。
问题是当我尝试将它与“键盘”模块一起使用时,它无法按预期工作:
class KeyboardHook(object):
def __init__(self):
self.thread = PrintHi()
self.thread.start()
self.set_keyboard_hotkeys()
def toggle_print(self):
print("Toggle Print")
self.thread.active = not self.thread.active
def set_keyboard_hotkeys(self):
print("Setting hotkeys hooks")
keyboard.add_hotkey('ctrl+c', self.toggle_print)
keyboard.wait()
if __name__ == '__main__':
hook = KeyboardHook()
这些是步骤:
线程本身工作正常,键盘挂钩本身也工作正常,但是当我将两者结合起来时,它们不起作用。
有谁知道我做错了什么? 有没有使用键盘快捷键打开和关闭线程的方法? 在我的应用程序中,我将有多个线程,我必须独立地打开和关闭它们。
谢谢!
我建议稍微重构您的代码,即在打印机线程中使用Event
而不是bool
变量来发出打印操作的信号,并添加允许您在程序退出时停止打印机线程的逻辑:
import time
from threading import Thread, Event
import keyboard
class PrintThread(Thread):
def __init__(self):
super().__init__()
self.stop = False
self.print = Event()
def run(self):
while not self.stop:
if self.print.wait(1):
print('Hi,', time.time())
def join(self, timeout=None):
self.stop = True
super().join(timeout)
另外,我建议将阻塞代码从KeyboadHook
初始化程序移出到单独的start
方法:
class KeyboardHook:
def __init__(self):
self.printer = PrintThread()
self.set_keyboard_hotkeys()
def toggle_print(self):
print('Toggle the printer thread...')
if self.printer.print.is_set():
self.printer.print.clear()
else:
self.printer.print.set()
def set_keyboard_hotkeys(self):
print('Setting keyboard hotkeys...')
keyboard.add_hotkey('ctrl+p', self.toggle_print)
def start(self):
self.printer.start()
try:
keyboard.wait()
except KeyboardInterrupt:
pass
finally:
self.printer.join()
像这样运行它:
hook = KeyboardHook()
hook.start()
这段代码对我来说就像一个魅力。
KeyboardHook 中的一些更改使其运行:
class KeyboardHook(object):
def __init__(self):
self.thread = PrintHi()
self.set_keyboard_hotkeys()
self.thread.start()
def toggle_print(self):
print("Toggle Print")
self.thread.active = not self.thread.active
def set_keyboard_hotkeys(self):
print("Setting hotkeys hooks")
keyboard.add_hotkey('ctrl+p', self.toggle_print)
#keyboard.wait()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.