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.

asked Apr 23, 2014 at 19:16

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()
answered Apr 23, 2014 at 19:25
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")

answered Apr 23, 2014 at 19:23

2 Comments

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.