0

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.

Madhav
  • 35
  • 11
  • does this answer Your question: https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-script-from-another-script also I think You will have to just follow the idea of importing the necessary functions running them in the main file and while that is happening halt the execution of the functions You don't need. There is also no need to run another python file because You can just import stuff, also the reason it stopped responding is because it was waiting for the `os.system()` to execute (which would happen when the second process would be terminated) – Matiiss Apr 18 '21 at 01:00
  • the link you shared just helped in opening a new window. But did not close the existing one. So now what I figured out is to create a new thread to the GUI_Interface() and then sys.exit for the existing one. This helped in solving the problem. – Madhav Apr 18 '21 at 23:02

2 Answers2

0

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

Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • it does not really work. If I try to execute it.. It just hangs the tkinter window. It does open the another one but both primary and secondary tkinter windows are just hanged. – Madhav Apr 16 '21 at 22:56
  • could You show exactly how You applied this method? just add to the existing question because exit should have exited the file – Matiiss Apr 17 '21 at 22:10
0

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

Madhav
  • 35
  • 11