1

I am trying to make a small GUI program with tkinter. I need to use Python's

subprocess.call()

When I try to do so inside a class method I get following error:

    self.return_code = self.subprocess.call("echo Hello World", shell=True)
AttributeError: 'App' object has no attribute 'subprocess'

Here is part of my program:

from tkinter import *
from subprocess import call

class App:
    def __init__(self, mainframe):
        self.mainframe = ttk.Frame(root, padding="10 10 12 12", relief=GROOVE) 
        self.mainframe.grid(column=0, row=1, sticky=(N, W, E, S))

        self.proceedButton = ttk.Button(self.mainframe, text="Proceed", command=self.proceed)
        self.proceedButton.grid(column=0, row=9, sticky=(W))

    def proceed(self):
        self.proceedButton.config(state=DISABLED)
        self.return_code = self.subprocess.call("echo Hello World", shell=True)

The last line inside the proceed function throws the error.

I am learning Python. Any guidance would be appreciated.

Aashiq Hussain
  • 543
  • 2
  • 8
  • 17

1 Answers1

3

Try subprocess.call instead of self.subprocess.call:

import subprocess
self.return_code = subprocess.call("echo Hello World", shell=True)

self is an instance of App. subprocess is a module. To understand why self.subprocess is wrong, read the "Random Remarks" from the Python tutorial on Classes. Then read about modules, and how to call a module's functions.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • This trows another error: self.return_code = subprocess.call("echo Hello World", shell=True) NameError: global name 'subprocess' is not defined This Works: self.return_code = call("echo Hello World", shell=True) – Aashiq Hussain Mar 23 '13 at 21:43
  • Indeed, if you use `from subprocess import call`, then you can write `call(...)` instead of `subprocess.call(...)`. I tend to [always import modules](http://stackoverflow.com/q/1744258/190597), however. – unutbu Mar 23 '13 at 21:46