# -*- coding: utf-8 -*-
# @Time : 2023/11/6 10:03
# @Author : wangyafeng
# @FileName: 进程和线程2.py
# @Email : yafengwang@dingtalk.com
# @Software: PyCharm
import threading
import multiprocessing
import PySimpleGUI as sg
import time
def task1():
"""任务1"""
while True:
print("Task 1 is working...")
time.sleep(1)
def task2():
"""任务2"""
while True:
print("Task 2 is working...")
time.sleep(1)
# 创建一个布局
layout = [[sg.Output(size=(40, 10))],
[sg.Button('Start Tasks1'),sg.Button('Start Tasks2'), sg.Button('Exit')]]
# 创建一个窗口
window = sg.Window('PySimpleGUI Multiple Tasks Example', layout)
# 创建一个线程和进程
thread = None
#process = None
# 按钮点击事件处理程序
while True:
event, values = window.read()
if event == 'Exit' or event == sg.WIN_CLOSED:
break
if event == 'Start Tasks1':
if thread and thread.is_alive():
continue # 防止多次点击按钮创建多个线程和进程
thread1 = threading.Thread(target=task1)
thread1.start()
if event == 'Start Tasks2':
if thread and thread.is_alive():
continue # 防止多次点击按钮创建多个线程和进程
thread2 = threading.Thread(target=task2)
thread2.start()
window.close()
标签:thread,PySimpleGUI,Start,import,多线程,sg,event From: https://www.cnblogs.com/wangyafeng/p/17812351.html