python脚本仅在我关闭控制台时才有效(python script only works when I close the console)

编程入门 行业动态 更新时间:2024-10-27 10:22:38
python脚本仅在我关闭控制台时才有效(python script only works when I close the console)

我正在使用Tkinter创建一个接口,将脚本连接到解释器,该角色将运行这些脚本。 我正在使用子进程模块popen(),它工作正常,但运行的结果显示在控制台窗口中。 我尝试将结果重定向到我界面中的文本区域,但控制台窗口仍然弹出,但这次是空的,只有在我关闭控制台窗口后,结果才显示在此文本区域中。 任何人都可以帮我弄清楚我做错了什么? 先谢谢你。

def interpreter1(): b=que2.get(block=True) a=que1.get(block=True) print "prameters are :" ,a ,b ,"\r\n" c='python C:\\workspace\\Project_Interpreter\\Tool-v1.0.py -s %s %s'%(b,a) ps=sp.Popen(['cmd','/K',c] , creationflags=0, shell=False, stdout=PIPE,stderr=sp.STDOUT,stdin=PIPE) texte01= ps.stdout.readlines()

I am using Tkinter to create an interface that connects a script to the interpreter whom role is to run these scripts. I am using the subprocess module popen(), and it works fine but the result of the running is shown in a console window. I tried redirecting the result to a text zone in my interface, but the console window is still popping out but this time it is empty and the result shows up in this text zone only after I close the console window. Anybody can help me figure out what I did wrong ? thank you in advance.

def interpreter1(): b=que2.get(block=True) a=que1.get(block=True) print "prameters are :" ,a ,b ,"\r\n" c='python C:\\workspace\\Project_Interpreter\\Tool-v1.0.py -s %s %s'%(b,a) ps=sp.Popen(['cmd','/K',c] , creationflags=0, shell=False, stdout=PIPE,stderr=sp.STDOUT,stdin=PIPE) texte01= ps.stdout.readlines()

最满意答案

您可以使用subprocess.STARTUPINFO类来隐藏控制台窗口中的文本。 它可以作为选项传递给subprocess.Peopen()。 尝试这个:

def interpreter1(): b=que2.get(block=True) a=que1.get(block=True) print "prameters are :" ,a ,b ,"\r\n" c='python C:\\workspace\\Project_Interpreter\\Tool-v1.0.py -s %s %s'%(b,a) startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW ps=sp.Popen(['cmd','/K',c] , creationflags=0, shell=False, startupinfo=startupinfo, stdout=PIPE, stderr=sp.STDOUT, stdin=PIPE) texte01= ps.stdout.readlines()

You can use the subprocess.STARTUPINFO class to hide the text from the console window. It can be passed as an option to subprocess.Peopen(). Try this:

def interpreter1(): b=que2.get(block=True) a=que1.get(block=True) print "prameters are :" ,a ,b ,"\r\n" c='python C:\\workspace\\Project_Interpreter\\Tool-v1.0.py -s %s %s'%(b,a) startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW ps=sp.Popen(['cmd','/K',c] , creationflags=0, shell=False, startupinfo=startupinfo, stdout=PIPE, stderr=sp.STDOUT, stdin=PIPE) texte01= ps.stdout.readlines()

更多推荐

本文发布于:2023-07-26 18:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在我   控制台   时才   脚本   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!