【问题标题】:Tcl_AsyncDelete with pynput and pyautoguiTcl_AsyncDelete 与 pynput 和 pyautogui
【发布时间】:2020-09-17 15:27:51
【问题描述】:

在代码的简化版本中:

from pynput import keyboard
import time
import pyautogui


class Test:
    def __init__(self):
        self.paused = False

    def on_activate(self):
        if self.paused:
            pyautogui.alert(text='was paused', title='title', button='button')
            self.paused = False
        elif self.paused is False:
            pyautogui.alert(text='was not paused', title='title', button='button')
            self.paused = True


test = Test()
pyautogui.alert(text='test', title='title', button='button')
hotkey = keyboard.GlobalHotKeys({
    '<ctrl>+a': test.on_activate
})
hotkey.start()

while True:
    time.sleep(1)

我会收到错误Tcl_AsyncDelete: async handler deleted by the wrong thread

现在我明白这来自处理线程的问题,或者更确切地说是缺乏上述处理。我注意到如果代码在类声明下方没有 alert() 的情况下运行;从来没有这样的错误。

我相信我理解这是因为 pynput 在另一个线程上工作,而不是第一次 pyautogui 调用;但是,由于我不再使用 for 警报框,有没有办法在该线程中“正确关闭”它并在另一个线程上操作它?

我有点不知所措,非常感谢任何输入或帮助。

【问题讨论】:

  • 如果相关,这也在 Windows 10,python V 3.8.3 上运行

标签: python python-3.x python-multithreading pyautogui pynput


【解决方案1】:

我已经设法找到了一个解决方法。就目前而言,“解决方案”只是不使用 pyautogui 来处理这些提示,并为这些侦听器调用构建一个独特的 tkinter 框。我得到的结束代码是:

from pynput import keyboard
import time
import pyautogui
import tkinter as tk
import tkinter.messagebox as messagebox


def send_alert(text, title):
    root = tk.Tk()
    root.withdraw()
    messagebox.showinfo(title, text)
    root.destroy()


class Test:
    def __init__(self):
        self.paused = False

    def on_activate(self):
        if self.paused:
            send_alert('paused', 'title')
            self.paused = False
        elif self.paused is False:
            send_alert('not paused', 'title')
            self.paused = True


test = Test()
pyautogui.alert(text='test', title='title', button='button')

hotkey = keyboard.GlobalHotKeys({
    '<ctrl>+a': test.on_activate
})
hotkey.start()

while True:
    time.sleep(1)

【讨论】:

    猜你喜欢
    • 2019-02-02
    • 2021-05-22
    • 2021-03-12
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 2022-12-15
    • 2021-03-19
    • 2021-07-17
    相关资源
    最近更新 更多