1

I have a simple Tkinter GUI, with one button and when the button is pushed I want it to run another program that I have written in Python.

 def openProgram ():
     #open up MyProgram.py

 MGui = Tk()
 MGui.geometry('450x450')

 mbutton = Button(text = "Go", command = openProgram).pack()

Seems easy enough, maybe I am not searching the correct terms.

2 Answers 2

3

You can call functions defined in another file by importing that file.

reticulator.py:

def main():
    print "reticulating splines..."
    #do stuff here
    print "splines reticulated"

gui.py:

from Tkinter import *
import reticulator

def openProgram():
    #call the `main` function defined in the other file
    reticulator.main()

MGui = Tk()
MGui.geometry('450x450')

mbutton = Button(text = "Go", command = openProgram).pack()
MGui.mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

This is a much better answer than the accepted one. Calling another python program using os.system shows a lack of understanding of modular programming.
I want to open a Python program in its own window. Will this do that?
1

Try to use os.system:

import os os.system("MyProgram.py")

2 Comments

Perfect! That's all I needed.
What if MyProgram.py has to be executed in a virtualenv?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.