1

This is an extract from the code of a Quickly application I'm working on:

# Code for other initialization actions should be added here.

self.apachestart = self.builder.get_object("apachestart")
self.label1 = self.builder.get_object("label1")

def on_apachestart_clicked(self, widget):
    subprocess.call(['sudo', 'service', 'apache2', 'start'])   

Now when I press the button to start Apache (named apachestart), it prints to standard out in the terminal that the server was started:

* Starting web server apache2  [ OK ] 

How do I get that text output from the terminal to be shown in a label?

1

1 Answer 1

5

This is not really a Quickly question, but a pure Python one.

Have a look at the Subprocess documentation, especially the check_output method.

You could do something like this (untested):

def on_apachestart_clicked(self, widget):
    result = subprocess.check_output(['sudo', 'service', 'apache2', 'start'])
    if result.split('[ ')[1].startswith('OK'):
        text = "Success!"
    else:
        text = "Failed!"
    self.label1.set_text(text)

Maybe also catch the CalledProcessError in the call.

6
  • I tryed your sugesstion but i get an error in terminal : "IndentationError: expected an indented block" ?? when i use space to move the line I get another error : "if result.split('[ ')[1].startswith('OK'): NameError: name 'result' is not defined "?? What seems to be the problem? Anyway the example is good to what i want but what I reealy wanted is to print the terminal output text to my label1.THNKS!I wait for another suggestion if you like!! Dec 4, 2012 at 23:00
  • 1
    Please, read a Python tutorial first before starting programming in the wild. Or atleast learn how to Google your error messages. An IndendationError means you're messing up your indentation (ah yes). Never mix tabs and spaces, follow PEP8 and use 4 spaces to indent. Remember, Python uses indentation to mark new code lines etc, not curly braces and semi-colons like many other languages. So it's impossible to say what you're doing wrong here, you'll have to check for yourself, or if you really, really, really can't figure it out, post the entire code.
    – Timo
    Dec 4, 2012 at 23:35
  • Now I'm able to open window but when I click on button I get another error in terminal : code [sudo] password for caezsar: Traceback (most recent call last): File "/home/caezsar/test/test/TestWindow.py", line 40, in on_apachestart_clicked if result.split('[ ')[1].startswith('OK'): IndexError: list index out of range Dec 5, 2012 at 1:12
  • What seems to be wrong?I'm new in python and sure need some professional help!can we talk on yahoo messenger ? caezsar is my id if you wanna give a help hand! THNKS! Dec 5, 2012 at 1:15
  • If you need some professional help, get professional help! For example, there are training classes available. See this link and this link.
    – Flimm
    Dec 5, 2012 at 15:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .