python gui 多线程测试
已有 402 次阅读2022-1-6 22:05
|系统分类:兴趣爱好
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import PySimpleGUIQt as sg
import threading, time
def make_win(idx):
layout=[[sg.Text('My one-shot window.'), sg.Text(size=(15,1), key='-OUTPUT-')],
[sg.InputText(key='-IN-')],
[sg.Submit(), sg.Button('Show'), sg.Cancel()]]
win=sg.Window("win"+str(idx), layout, font=('宋体', 15),finalize=True) #,default_element_size=(120,5)
return win
class myThread (threading.Thread): #继承父类threading.Thread
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.ok = 0
#self.timer=QTimer(self)
def run(self): #把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
self.win = make_win(self.name)
self.ok = 1
while True:
time.sleep(0.02)
if self.win == None:
return
def start_gui():
while True:
window, event, values = sg.read_all_windows()
print(window, event)
if window == None: # if all windows were closed
break
if event == sg.WIN_CLOSED or event == 'Exit':
window.close()
continue
if event in (None, 'Submit'):
break
if event == 'Show':
window['-OUTPUT-'].update(values['-IN-'])
#if None == values:
# text_input = 'Empty'
#else:
# text_input = values['-IN-']
#sg.popup('You entered', text_input)
if __name__ == "__main__":
wins = []
for i in range(2):
wins.append(myThread(i, "Thread-1"))
for th in wins:
th.start()
while th.ok == 0:
time.sleep(0.01)
continue
start_gui()