[英]How can I run my python script while a Tkinter GUI is active?
我正在尝试创建一个GUI。 GUI处于活动状态时,我需要执行另一个python脚本。 (该GUI应该处理此执行中的数据)因为我使用Tkinter创建了GUI,所以无法在python终端中执行另一个文件。
我怎么解决这个问题?
您无需启动其他解释器。
您可以简单地从separte线程或进程中的其他脚本执行代码。
您希望您的“其他”脚本可从另一个脚本调用。 为此,您只需要一个功能即可执行脚本曾经执行的操作。
#other.py
def main(arg1, arg2, arg3):
do_stuff(arg1, arg2)
more_stuff(arg2, arg3)
other_stuff(arg1, arg3)
finish_stuff(arg1, arg2, arg3)
在主脚本中,当您想从other.py
执行代码时,启动一个新线程:
#script.py
from threading import Thread
from other import main
thread = Thread(target = main)
thread.start() # This code will execute in parallel to the current code
要检查您的工作是否完成,请使用thread.is_alive()
。 要阻塞直到完成,请使用thread.join()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.