【发布时间】:2020-09-26 03:16:43
【问题描述】:
我在 Mac 上的单独线程中使用 PyAutoGui 运行按键时遇到问题。 这是我得到的错误:
Python[39499:497432] pid(39499)/euid(501) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
就像消息说的那样,当我在主线程中运行它时它工作正常。但我更愿意在单独的线程中执行该操作,这样它就不会阻塞 UI。有没有办法解决这个问题?它看起来在 Windows 机器上运行良好。
信息
- Python:3.8.3
- 操作系统:Mac MoJave 10.14.6
- TkInter:8.5
要重现,请运行以下代码并在弹出的窗口中单击Start 按钮。
import threading
import pyautogui
import tkinter as tk
from tkinter import ttk
def press_key():
pyautogui.press('a')
def handle_button_press():
t = threading.Thread(target=press_key)
t.start()
def main():
root = tk.Tk()
root.title('Crash')
content = ttk.Frame(root, width=200, height=200)
button = tk.Button(content, text='Start', command=handle_button_press)
# Layout
content.grid(column=0, row=0, sticky=(tk.N, tk.E, tk.S, tk.W))
button.grid(column=0, row=0, sticky=(tk.N, tk.E, tk.S, tk.W))
content.rowconfigure(0, weight=1)
content.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.mainloop()
if __name__ == '__main__':
main()
【问题讨论】:
标签: python multithreading macos tkinter pyautogui