0

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()

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

11 Comments

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: 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.
and does this way open it in another window
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 ?
With something like: b = Button(master, text="Run FILE_1", command=file1_callback), clicking Button b would cause file1_callback() to be run. See effbot.org/tkinterbook/button.htm
|

Your Answer

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.