繁体   English   中英

在 function 运行时用户单击 GUI 后避免 GUI 崩溃?

[英]Avoid GUI crash after user clicks GUI while a function is running?

因此,我使用PySimpleGUI创建了一个简单的 GUI,单击按钮后运行一个脚本,该脚本在特定时间间隔后左右移动 cursor。 但是,脚本仅在用户按下Ctrl + 1时停止。 现在,当脚本运行时,如果用户单击 GUI,程序将转到无响应state。我想避免这种情况。 下面是一段解释我的主要代码的代码片段:

import PySimpleGUI as sg

def show_mainWindow():
    
    mainLayout = [
        [sg.Button("Start Script", size=(50,0))],
        [sg.Text("Press to Start"), sg.Button("Settings")]
    ]

    mainWindow = sg.Window("GUI", mainLayout, size=(290,75))

    delay = 0

    while True:
        event, values = mainWindow.read()
        if event == sg.WIN_CLOSED:
            print("Close")
            break
        elif event == "Settings":
            delay = show_settingsWindow()
        if event == "Start Script":
            if delay == 0:
                mouse_thread = threading.Thread(target=keep_me_alive(6))
                mouse_thread.start()
            else:
                mouse_thread = threading.Thread(target=keep_me_alive(int(delay)))
                mouse_thread.start()

    mainWindow.close()

show_mainWindow()

请注意,未发布keep_me_alive的代码,因为脚本本身很好,只是在用户按下Ctrl + 1之前的while True循环中。

参数target的值是 function,而不是 function 的结果。

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)
        if event == "Start Script":
            if delay == 0:
                mouse_thread = threading.Thread(target=keep_me_alive, args=(6,), daemon=True)
                mouse_thread.start()
            else:
                mouse_thread = threading.Thread(target=keep_me_alive, args=(int(delay),), daemon=True)
                mouse_thread.start()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM