python子进程读取终端输出并使用PyQt4消息弹出回复 [英] python subprocess reading terminal output and reply using PyQt4 message pop up

查看:57
本文介绍了python子进程读取终端输出并使用PyQt4消息弹出回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来检查 python 中的 adb 设备

Hi im making a program to check adb devices in python

我无法弹出窗口,请更正我的代码.

I cant make a pop up window please correct my code.

导入系统导入子流程从 PyQt4 导入 QtGui、QtCore

import sys import subprocess from PyQt4 import QtGui, QtCore

ad = subprocess.check_output(["adb", "devices"])

ad = subprocess.check_output(["adb", "devices"])

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))


        btn = QtGui.QPushButton('Check Device', checkadb(ad))
        btn.resize(135, 30)
        btn.move(50, 10)

        self.setGeometry(250, 250, 400, 400)
        self.setWindowTitle('Tooltips')
        self.show()

def clicked(*args):
    QtGui.QMessageBox.about(QtGui, "No Device Found")

def clicked2(*args):
    QtGui.QMessageBox.about(QtGui, "Device Found")

def checkadb(ad):
    if len(ad) <= 27:
        clicked()

elif len(ad) > 27:
    clicked2()

def main():

app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

if __name__ == '__main__':
    main()

推荐答案

多一点上下文可能会有所帮助;您在将输出输入代码时遇到问题吗?如果是这样,您可以考虑使用 subprocess.Popen(...) 代替.它需要一个参数列表,并且可以轻松地从指定的源获取输出.例如,如果您想运行ls myDir",您可以使用:

A little more context might help; are you having trouble getting the output into your code? If so, you might consider using subprocess.Popen(...) instead. It takes a list of arguments, and can easily get output from a specified source. For example, if you wanted to run 'ls myDir', you could use:

adbCmd = subprocess.Popen(['adb', 'devices'], stdout=PIPE)

然后您可以使用 Popen 对象的communication() 方法来确保命令运行完成并获得stdout 和stderr:

Then you can use the Popen object's communicate() method to ensure the command runs to completion and get stdout and stderr:

adbOut, adbErr = adbCmd.communicate()

或者,如果你只想要标准输出:

Or, since communicate returns a tuple, if you only want stdout:

adbOut = adbCmd.communicate()[0]

然后就可以正常解析找到你想要的数字了.

Then you can parse normally to find the number you want.

这篇关于python子进程读取终端输出并使用PyQt4消息弹出回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆