Mat*_*zar 1 python application-development
这是我正在处理的 Quickly 应用程序代码的摘录:
# 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'])
Run Code Online (Sandbox Code Playgroud)
现在,当我按下按钮启动 Apache(名为apachestart
)时,它会在服务器启动的终端中打印为标准输出:
* Starting web server apache2 [ OK ]
Run Code Online (Sandbox Code Playgroud)
如何从终端获取要显示在标签中的文本输出?
这不是一个真正的 Quickly 问题,而是一个纯 Python 问题。
查看Subprocess
文档,尤其是check_output
方法。
你可以做这样的事情(未经测试):
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)
Run Code Online (Sandbox Code Playgroud)
也许也能CalledProcessError
接听电话。
归档时间: |
|
查看次数: |
151 次 |
最近记录: |