Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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):['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?

share|improve this question
Have a look at this answer: – Flimm Dec 5 '12 at 15:27

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!"
        text = "Failed!"

Maybe also catch the CalledProcessError in the call.

share|improve this answer
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!! – Matei Cezar Dec 4 '12 at 23:00
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 '12 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/", line 40, in on_apachestart_clicked if result.split('[ ')[1].startswith('OK'): IndexError: list index out of range – Matei Cezar Dec 5 '12 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! – Matei Cezar Dec 5 '12 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 '12 at 15:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.