退出主.py文件时无法打开另一个.py文件



我已经创建了 2 个.py文件。 我希望一旦主文件关闭,第二个文件就会使用 tkinter 打开。这是 tkinter 按钮的多个命令的延续 .

我写的退出按钮功能是:-

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 和辅助 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)

这是我根据给出的解决方案实现的。

所以我做了一些编码并提出了这个相当有趣的解决方案(当然应该在Windows上工作,不知道其他操作系统):

import os
path_to_dir = os.getcwd()
file_name = 'to_run_file.py'

def close():
os.system(f'cd "{path_to_dir}"')
os.system(f'python {file_name}')
exit()

close()

所以基本上发生的事情是首先你得到你想要运行的文件所在的目录的路径(如果在另一个子目录中添加到路径),然后你还指定"to_run_file.py"这是你将运行的那个,然后cmd完成剩下的,你可以退出当前进程。但是,我只是建议将函数导入文件并在当前文件中运行该函数,只需停止您想要的进程并调用导入的函数

所以基本上我已经能够通过创建另一个线程来GUI_Interface然后关闭现有文件来解决这个问题。

最新更新