Python Forum
Button in GUI linked to new python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Button in GUI linked to new python script
#5
(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
Reply


Messages In This Thread
RE: Button in GUI linked to new python script - by blakefindlay - Jan-30-2021, 02:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Kivy] Two Linked Kivy Windows? Exsul1 0 1,338 Mar-04-2020, 07:32 PM
Last Post: Exsul1
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,080 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  how to execute .py script when button is clicked D_frucht 1 6,218 Jun-22-2018, 04:23 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020