Aashiq Hussain
Aashiq Hussain

Reputation: 583

How can I invoke a function from an imported module inside a class methode?

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.

Upvotes: 1

Views: 91

Answers (1)

unutbu
unutbu

Reputation: 879471

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.

Upvotes: 3

Related Questions