PySimpleGUI+多线程

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

 

posted @   王亚锋  阅读(124)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示