views:

112

answers:

1

I am writing a tkinter program that is kind of a program that is like a portfolio and opens up other programs also writen in python. So for example i have FILE_1 and FILE_2 and i want to write a program that onced clicked on a certain button opens either FILE_1 or FILE_2. i dont need help with the look like with buttons just how to wirte a function that opens a program

This is the code i used:

from Tkinter import *
import subprocess

master = Tk()

def z():
    p=subprocess.Popen('test1.py')
    p.communicate()


b = Button(master, text="OK", command=z)
b.pack()



mainloop()
+3  A: 

Hook the button up a callback which calls subprocess.Popen:

import subprocess
p=subprocess.Popen('FILE_1.py')
p.communicate()

This will try to run FILE_1.py as a separate process. p.communicate() will cause your main program to wait until FILE_1.py exits.

unutbu
thanks but what if i want it to have two buttons wouldn't they both open the same file or is there a way to distinguish between files?
Matthew Carter
@Matthew: Each button can be connected to its own callback function. So pressing each button would invoke a different function which could run a different program.
unutbu
and does this way open it in another window
Matthew Carter
what i mean is if i call subprocess.Popen it will open FILE_1 and lets say i had another function called G=subprocess.Popen('FILE_2.py')woould i call subprocess.G ?
Matthew Carter
With something like: `b = Button(master, text="Run FILE_1", command=file1_callback)`, clicking Button `b` would cause `file1_callback()` to be run. See http://effbot.org/tkinterbook/button.htm
unutbu
Is `FILE_1` a python program? Is it a GUI program? or a text-based program? Or have I got it all wrong and it is a text file that whose contents you wish displayed?
unutbu
OH wow im so stupid i wanst thinking lol thanks.
Matthew Carter
no no the are python command programs and some are GUI. So i want them to be opened in another window. Will this also work for like a mp3 file to open seperatly or no
Matthew Carter
If you can launch the programs from the command-line with `python FILE_1.py` then that is what you could use. For example, `subprocess.Popen('python FILE_1.py')`.
unutbu
maybe im not getting it im posting the code i used and it threw an error
Matthew Carter
okay the code is up top
Matthew Carter
Try giving a full path to test1.py, or make sure test1.py is in a directory your OS searches for apps. (I'm being non-specific because I don't know if you are using Windows or Linux or something else). You also may need to use `python test1.py`. If neither of these help, please post the error message.
unutbu
sweet thanks it worked i needed to put the full path and write python (i.e. 'python C:/Users/matthew/Desktop/test3.pyw') thanks a lot and BTW im using Vista
Matthew Carter