PySimpleGUI 不会将 multiprocessing 的 cprint 重新路由到其 Multiline 元素
我正在使用 multiprocessing
模块和 PySimpleGUI
。
所有内容都打印在我的 IDE 控制台中,而不是 Multiline
。仅当我使用多处理
时才会出现此问题。其他带有 sg.cprint 语句的函数,甚至带有简单的 print 语句的函数也会以 Multiline 的形式打印结果,就像它应该的那样。
这是我的代码,它显示了我尝试使用 reroute_stdout=True
将所有内容重新路由到 Multiline
的问题
import PySimpleGUI as sg
import multiprocessing
def func(link: str):
sg.cprint(link)
sg.cprint('string')
print(link)
print('string')
def make_window():
layout = [[sg.Multiline(key='-Multiline-', reroute_stdout=True)],
[[sg.Button('Start', key='-Start-')]]]
window = sg.Window('Forum', layout, finalize=True)
sg.cprint_set_output_destination(window, '-Multiline-')
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == '-Start-':
sg.cprint('Process has started')
process = multiprocessing.Process(target=func,
args=('https://stackoverflow.com/questions/ask',),
daemon=True)
process.start()
window.close()
if __name__ == '__main__':
make_window()
- 不起作用。 根据 PySimpleGUI
的所谓“Cookbook”,可以像这样重新路由打印:
window['-Multiline-'].print('Testing 1 2 3')
如果我在函数中放入类似的内容,它就不起作用(我认为这是因为我的函数位于GUI 代码)
总之 - 当我使用 threading
模块时,问题不会出现。但是multiprocessing
解决了其他问题 - 它允许我使用.terminate()
终止进程。使用线程
无法轻松做到这一点。
我的应用程序使用 OOP,比我提供的代码稍微复杂一些。
I am using multiprocessing
module along with PySimpleGUI
.
Instead of Multiline
everything gets printed in my IDE console. This problem only happens when I use multiprocessing
. Other function with sg.cprint
statement and even with simple print
statement would print the result in Multiline
, as it should.
Here's my code, that shows the problem
import PySimpleGUI as sg
import multiprocessing
def func(link: str):
sg.cprint(link)
sg.cprint('string')
print(link)
print('string')
def make_window():
layout = [[sg.Multiline(key='-Multiline-', reroute_stdout=True)],
[[sg.Button('Start', key='-Start-')]]]
window = sg.Window('Forum', layout, finalize=True)
sg.cprint_set_output_destination(window, '-Multiline-')
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == '-Start-':
sg.cprint('Process has started')
process = multiprocessing.Process(target=func,
args=('https://stackoverflow.com/questions/ask',),
daemon=True)
process.start()
window.close()
if __name__ == '__main__':
make_window()
I have tried to reroute everything to Multiline
with reroute_stdout=True
— doesn't work.
According to so called "Cookbook" of PySimpleGUI
, it's possible to reroute print like this:
window['-Multiline-'].print('Testing 1 2 3')
It doesn't work if I put something like that in my function (I assume that is because my function is above the GUI code)
In conclusion - the issue doesn't appear when I use threading
module. But multiprocessing
solves other problem - it allows me to terminate a process with .terminate()
. Couldn't do that with threading
as easily.
My application uses OOP and a bit more complicated than the code I provided.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对库
multiprocessing
一无所知,遵循代码工作,但可能并不完美。I know nothing about library
multiprocessing
, following code work, but maybe not perfect.