【发布时间】:2021-07-04 19:05:26
【问题描述】:
我创建了 2 个.py 文件。我希望一旦主文件关闭,第二个文件使用 tkinter 打开。这是multiple commands for a tkinter button的延续
.
我写的退出按钮功能是:-
from Backup_Server import GUI_Interface
def close_func():
os.kill(os.getpid(), signal.SIGINT)
GUI_Interface()
window.destroy()
server_socket.close()
if __name__ == "__main__":
exit_button = Button(topFrame, text='Quit', command=close_func)
GUI_Interface 是我需要在关闭现有的.py 文件后调用的函数。如果我把GUI_Interface 作为close_func() 的第一个命令,那么它真的不会回到第二步,也不会关闭现有的.py 文件。
如果我把GUI_Interface放在最后,它只会关闭现有的,永远不会打开新的.py文件的功能。
编辑:-
尝试实现给定的解决方案,但它只是挂起主要和次要 Tkinter 窗口:_
path_to_dir = os.getcwd()
print("path of file:;:\n", path_to_dir)
file_name = 'Backup_Server.py'
def close_func():
os.system(f'cd "{path_to_dir}"')
os.system(f'python {file_name}')
exit()
exit_button = Button(topFrame, text='Quit', command=close_func)
这是我根据给出的解决方案实现的。
【问题讨论】:
-
这个回答你的问题:stackoverflow.com/questions/1186789/… 我也认为你将不得不遵循导入在主文件中运行它们的必要函数的想法,而当这种情况发生时,停止函数的执行你不需要。也不需要运行另一个python文件,因为您可以只导入东西,它停止响应的原因也是因为它正在等待
os.system()执行(这将在第二个进程终止时发生) -
您分享的链接刚刚帮助打开了一个新窗口。但没有关闭现有的一个。所以现在我想出的是为 GUI_Interface() 创建一个新线程,然后为现有线程创建一个 sys.exit。这有助于解决问题。
标签: python python-3.x tkinter client-server failover