1

I am trying to run a .py file from another python code using the following sequence.

from tkinter import *
import os
import sys

def open_SS():
    print('processing')
    os.system('cd /home/pi/Desktop/Backup')
    os.system('python p.py')

def close_window(): 
    master.destroy()

master = Tk()

master.minsize(width=500, height=300)

Button(master, text='Exit', command=close_window).grid(row=12, column=6, sticky=W, pady=4)
Button(master, text='SS', command=open_SS).grid(row=12, column=8, sticky=W, pady=4)

mainloop( )

The Exit button does the command, but the 'SS' button does not, the word 'processing' does get printed, just the running of the p.py file. I tried running those two os.system commands on a command terminal and it works fine. p.py is supposed to input GPIO signals to a Raspberry Pi.

3
  • why not simply import p? Commented Mar 20, 2018 at 7:37
  • 3
    Also, cd ... and python ... need to be in the same os.system invocation, for example os.system('cd ...; python ...'). If you separate them, they are executed in separate shells, and the cd performed in the first os.system() does not affect the second one. Commented Mar 20, 2018 at 7:57
  • Try fully qualified path of your installed python as well as the file in the same command. Commented Mar 21, 2018 at 10:12

1 Answer 1

2
from tkinter import *

import os

import sys

master = Tk()

def open_SS():

    print('processing')
    os.system("python /home/pi/Desktop/Backup/p.py")

btn=Button(master,text="click here")

btn.grid(row=0,column=1)

btn.bind("<Button>",open_SS)
Sign up to request clarification or add additional context in comments.

4 Comments

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
I had to make a few changes for it to work. This code kept bringing up the error TypeError: worker() takes 0 positional arguments but 1 was given. To take care of this, a parameter needs to be given, according to this link. In the end I used def open_SS(self): import p.py because os.system just did not work.
def open_SS(event):
some changes in function line

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.