Open File with Python
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():开发者_JS百科
p=subprocess.Popen('test1.py')
p.communicate()
b = Button(master, text="OK", command=z)
b.pack()
mainloop()
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.
精彩评论