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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM