zoukankan      html  css  js  c++  java
  • PyQt5界面上调用subprocess.Popen会闪命令窗口的问题

    最近再做一个界面开发,主要实现的点击一个按钮,会执行adb安装应用程序的功能,在调试阶段一切都正常,但打包成一个exe安装程序,安装之后运行,点击按钮会闪一下adb的命令窗口

    先列出subprocess.Popen方法介绍,里面有很多关键字参数

    一、subprocess.Popen
    subprocess模块定义了一个类: Popen
    class subprocess.Popen( args,
    bufsize=0,
    executable=None,
    stdin=None,
    stdout=None,
    stderr=None,
    preexec_fn=None,
    close_fds=False,
    shell=False,
    cwd=None,
    env=None,
    universal_newlines=False,
    startupinfo=None,
    creationflags=0)

     

    划重点:

    cmd_test = "adb install xxx.apk"

    subprocess.Popen(cmd_test, shell=True)
    这是因为它相当于
    subprocess.Popen(["cmd.exe", "-c", cmd_test])
    在*nix下,当shell=False(默认)时,Popen使用os.execvp()来执行子程序

    所以如果不设置shell这个参数时,会默认会启动系统的命令窗口来显示操作,要不让它使用系统的,就将shell=True即可,就不会闪一下命令窗口的问题了。

    将代码重新修改一下再次打包运行,果然就不会闪命令窗口界面了

     1     def install_app(self, file):
     2         try:
     3             install_cmd = r"adb install -g -t -r {}".format(file)
     4             log.debug("The install command is: {}".format(install_cmd))
     5             result_output = subprocess.Popen(install_cmd, stdout=subprocess.PIPE, shell=True)
     6             result_lst = result_output.stdout.readlines()
     7             for item in result_lst:
     8                 item_strip = item.decode("gbk").strip()
     9                 log.debug(item_strip)
    10                 if "Success" == item_strip:
    11                     return True
    12             else:
    13                 return False
    14 
    15         except Exception as e:
    16             log.debug(e)
  • 相关阅读:
    按钮常用
    MySQL常用Json函数
    MySQL所有函数及操作符
    MySQL常用时间函数
    MySQL常用聚合函数
    Shiro整合Spring
    Shiro集成Web
    Shrio授权验证详解
    Shrio认证详解+自定义Realm
    Shiro入门
  • 原文地址:https://www.cnblogs.com/aziji/p/12091844.html
Copyright © 2011-2022 走看看