Search code examples
pythonpython-3.xtkintercmdpycharm

Tkinter Not responding after entering cmd command with os module


I have a button that runs a function. This function is supposed to run 2 cmd commands, and the first cmd command works fine, but the tkinter GUI freezes before the second command can be executed. Is there a way to fix this?

Using pycharm and python 3.9

def runspotdl():
    os.system(f'cmd /k "my command here"')
    os.system(f'cmd /k "my second command here"')

Solution

  • With /k cmd executes and then remains open.

    With /c cmd executes and closes.

    So, your 1st cmd window upon execution executes and then waits until you close it, and just as you close it, your 2nd cmd command will be executed in the new cmd window. Also, by all this time, your Tkinter window will freeze since its program is still running for cmd windows. It will unfreeze just as you exit the 2nd cmd window.

    So, what I would suggest is, if you are not displaying anything in cmd window using 1st command, then use /c for the 1st command and /k for the 2nd (to keep it open until you want to exit. Or just add /c in every command close the cmd windows after their execution.) e.g.

    def runspotdl():
        os.system(f'cmd /c "1st cmd command"')
        os.system(f'cmd /k "2nd cmd command"')