Python Forum
Button in GUI linked to new python script - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Button in GUI linked to new python script (/thread-32229.html)



Button in GUI linked to new python script - blakefindlay - Jan-29-2021

I am wondering if it's possible to set a button to open a new script which will subsequently open a second tkinter gui?


RE: Button in GUI linked to new python script - deanhystad - Jan-29-2021

If you can do something in Python you can have the button call the Python code that does the something. Starting with a crude way of doing things you could execute a shell command.
import os

def run_program():
    os.system('python my_python_program.py')

Button(root, text='Run My Program', command=run_program)
A cleaner way to do this is use subprocess. This can be very simple.
import subprocess

def run_program():
    subprocess.call(["python", "my_python_program.py"])

Button(root, text='Run My Program', command=run_program)
Or you can use subprocess.Popen() which returns a handle that you can use to monitor the subprocess. You can even open up pipes so you can communicate with the subprocess.

But maybe you don't need another process. Why do you think you need to start up another GUI? What are you trying to do?


RE: Button in GUI linked to new python script - Axel_Erfurt - Jan-29-2021

Import your second script and use the code then.
Save it in the same folder within your first script.

for example, if file name second_script.py, do

import second_script


RE: Button in GUI linked to new python script - deanhystad - Jan-29-2021

Import will not work if the imported script is meant to launch another GUI application. Two tkinter applications must be two separate applications. You cannot call Tk() twice in the same application and you cannot call mainloop() twice in the same application. At least you can't without a lot of caution (somehow IDLE makes it work).


RE: Button in GUI linked to new python script - blakefindlay - Jan-30-2021

(Jan-29-2021, 07:02 PM)deanhystad Wrote: If you can do something in Python you can have the button call the Python code that does the something. Starting with a crude way of doing things you could execute a shell command.
import os

def run_program():
    os.system('python my_python_program.py')

Button(root, text='Run My Program', command=run_program)
A cleaner way to do this is use subprocess. This can be very simple.
import subprocess

def run_program():
    subprocess.call(["python", "my_python_program.py"])

Button(root, text='Run My Program', command=run_program)
Or you can use subprocess.Popen() which returns a handle that you can use to monitor the subprocess. You can even open up pipes so you can communicate with the subprocess.

But maybe you don't need another process. Why do you think you need to start up another GUI? What are you trying to do?

Great thanks. I added in right before the subprocess.call but within the run_program function
root.destroy()
FYI, I am designing a rental form for ski equipment at work as that is our main rental and within this program I want the ability to open other less common rental forms like snowshoe and skates. This function suits it perfectly so thank you for the help


RE: Button in GUI linked to new python script - deanhystad - Jan-30-2021

Normally this would be done with one application with (possibly) multiple windows, or, more commonly, one window that changes views based on the operation.