Unable to open another .py file on exiting the primary .py file

Question:

I have created 2 .py files. i want that once the primary file closes, the second one opens using tkinter. This is a continuation of multiple commands for a tkinter button
.

The exit button functionality I have written is:-

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 is a function that I need to call after closing the existing .py file. If I put GUI_Interface as the first command for close_func(), then it really does not go back to the 2nd step and never closes the existing .py file.

And if I place GUI_Interface in the end, it just closes the existing one and never opens the function of the new .py file.

EDIT:-

Tried implementing the given solution but it just hangs both the primary and secondary Tkinter window:_

  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)

This is what I implemented as per the solution given.

Asked By: Madhav

||

Answers:

So I did some coding and came up with this rather interesting solution (should certainly work on windows don’t know about other OSs):

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()

So basically what happens is first You get the path to the directory where the file You want to run is located (if in another subdirectory add to path) and then You also specify the "to_run_file.py" which is the one You will run and then cmd does the rest and You can just exit the current process. However I would just recommend importing a function to the file and running the function in the current file, just stop the process You want to and call the imported function

Answered By: Matiiss

So basically I have been able to solve this problem by creating another thread to GUI_Interface and then closing the existing file.

Answered By: Madhav

Madhav I didn’t understand how you got the solution for this problem.
I am facing this same problem for a long time, Please help me to reach the solution…
can you elaborate it by showing the code…..

Answered By: JAYAKANTH ARUN