Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何阻止python脚本变得无响应?_Python_Macos - Fatal编程技术网

如何阻止python脚本变得无响应?

如何阻止python脚本变得无响应?,python,macos,Python,Macos,我对编程真的很陌生,我试着用我的知识制作一个自动点击应用程序,在按下f6的同时反复点击鼠标左键。我正在mac上编写和运行代码。我的问题是,这一切都很好,但当我发布f6时,它确实会停止按预期单击,但应用程序窗口会变得无响应,需要强制退出。有没有办法解决这个问题,因为它严重限制了功能 import tkinter as tk from pynput.keyboard import Key, Listener import pyautogui def clicker(): def on_p

我对编程真的很陌生,我试着用我的知识制作一个自动点击应用程序,在按下f6的同时反复点击鼠标左键。我正在mac上编写和运行代码。我的问题是,这一切都很好,但当我发布f6时,它确实会停止按预期单击,但应用程序窗口会变得无响应,需要强制退出。有没有办法解决这个问题,因为它严重限制了功能

import tkinter as tk
from pynput.keyboard import Key, Listener
import pyautogui


def clicker():
    def on_press(key):
        if key == Key.f6:
            pyautogui.click(button='left')

    def on_release(key):
        if key == Key.esc:
            # Stop listener
            return False

    with Listener(
            on_press=on_press,
            on_release=on_release) as listener:
        listener.join()


window = tk.Tk()

button = tk.Button(
    master=window,
    text='click me',
    command=clicker,
    height=10,
    width=20
)

button.pack()

window.mainloop()
更新代码:

import threading
import tkinter as tk
from pynput.keyboard import Key, Listener
import pyautogui


def helper():
    h = threading.Thread(target = clicker, daemon=True)
    h.start()


def clicker():
    def on_press(key):
        if key == Key.f6:
            pyautogui.click(button='left')

    def on_release(key):
        if key == Key.esc:
            # Stop listener
            return False

    with Listener(
            on_press=on_press,
            on_release=on_release) as listener:
        listener.join()


window = tk.Tk()

title = tk.Label(
    master = window,
    text = 'Autoclicker'
)

button = tk.Button(
    master=window,
    text='click me',
    command=helper,
    height=10,
    width=20
)

button.pack()
title.pack()



window.mainloop()

可以将该方法作为新线程启动:

import _thread


def helper():
    # helper function for thread call
    _thread.start_new_thread(clicker)

这将使主线程转到
mainloop
,并应防止窗口冻结。

感谢您的帮助!我并没有完全按照你的建议去做,但我确实使用了线程,我将在上面添加我的最终代码!:)