python-3.x 避免在功能运行时用户点击GUI后GUI崩溃?

vyu0f0g1  于 2023-01-27  发布在  Python
关注(0)|答案(1)|浏览(116)

因此,我使用PySimpleGUI创建了一个简单的GUI,在单击按钮时运行一个脚本,在特定的时间间隔后左右移动光标。但是,只有当用户按Ctrl + 1时,脚本才会停止。现在,当脚本运行时,如果用户单击GUI,程序进入一个不响应状态。2我想避免这种情况。3下面是一段代码,解释了我的主要代码:

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的代码没有发布,因为脚本本身很好,只是在while True循环中,直到用户按下Ctrl + 1

6l7fqoea

6l7fqoea1#

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

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()

相关问题