【发布时间】:2021-07-16 03:52:49
【问题描述】:
我从 PySimpleGUI 看到了一个示例演示代码,但我仍然不确定 window 变量是否如何
窗口作为参数传递给 the_thread() 函数,它设法对主函数内部的 window.write_event_value 等窗口变量执行一些操作。
我在 main 中有一个 cp(values) 证明它是被添加的。
谁能给我解释一下这怎么可能?
import threading
import time
import PySimpleGUI as sg
THREAD_EVENT = '-THREAD-'
cp = sg.cprint
def the_thread(window):
i = 0
while True:
time.sleep(1)
cp('This keeps on running')
window.write_event_value('-THREAD-', (threading.current_thread().name, i)) # Data sent is a tuple of thread name and counter
cp('This is cheating from the thread', c='white on green')
i += 1
def main():
layout = [ [sg.Text('Output Area - cprint\'s route to here', font='Any 15')],
[sg.Multiline(size=(65,20), key='-ML-', autoscroll=True, reroute_stdout=True, write_only=True, reroute_cprint=True)],
[sg.T('Input so you can see data in your dictionary')],
[sg.Input(key='-IN-', size=(30,1))],
[sg.B('Start A Thread'), sg.B('Dummy'), sg.Button('Exit')] ]
window = sg.Window('Window Title', layout)
while True: # Event Loop
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event.startswith('Start'):
threading.Thread(target=the_thread, args=(window,), daemon=True).start()
if event == THREAD_EVENT:
cp(f'Data from the thread ', colors='white on purple', end='')
cp(f'{values[THREAD_EVENT]}', colors='white on red')
cp(values)
window.close()
if __name__ == '__main__':
main()
【问题讨论】:
-
我试图回答你的问题,但不明白这是什么问题?
标签: python function pysimplegui