在 Windows 10 上使用 tkinter 在 GUI 的后台运行控制台窗口 [英] Running console window in background for GUI using tkinter on Windows 10

查看:55
本文介绍了在 Windows 10 上使用 tkinter 在 GUI 的后台运行控制台窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个用 tkinter 制作的 GUI,一切正常.它的作用是连接到服务器并为 Linux 或 Windows 发送命令.我继续使用 pyinstaller 创建一个没有控制台的窗口 GUI,当我尝试使用特定函数发送 Windows 命令时,它会失败.如果我使用在 GUI 之前弹出的控制台创建 GUI,它就像一个魅力.我想弄清楚的是如何让我的 GUI 与用户不可见的控制台一起工作.

So I have this GUI that I made with tkinter and everything works well. What it does is connects to servers and sends commands for both Linux or Windows. I went ahead and used pyinstaller to create a windowed GUI without console and when I try to uses a specific function for sending Windows commands it will fail. If I create the GUI with a console that pops up before the GUI, it works like a charm. What I'm trying to figure out is how to get my GUI to work with the console being invisible to the user.

我的代码中有问题的部分围绕子进程.为了让你们免于我编写的 400 多行代码,我提供了有问题的特定代码.这是片段:

The part of my code that has the issue revolves around subprocess. To spare you all from the 400+ lines of code I wrote, I'm providing the specific code that has issues. Here is the snippet:

def rcmd_in(server):
    import subprocess as sp
    for i in command_list:
        result = sp.run(['C:/"Path to executable"/rcmd.exe', '\\\\' + server, i],
                 universal_newlines=True, stdout=sp.PIPE, stderr=sp.STDOUT)
        print(result.stdout)

参数 'server' 是从另一个调用 'rcmd_in' 的函数传递过来的,'command_list' 是一个在代码根目录中创建的可变列表,所有函数都可以访问.

The argument 'server' is passed from another function that calls to 'rcmd_in' and 'command_list' is a mutable list created in the root of the code, accessible for all functions.

现在,我已经完成了尽职调查.我进行了多次搜索,并对我的代码进行了编辑,尝试在该控制台不可见的情况下运行我的代码,发现使用来自以下链接的信息:recipe-subprocess.下面是编辑的样子:

Now, I have done my due diligence. I scoured multiple searches and came up with an edit to my code that makes an attempt to run my code with that console invisible, found using info from this link: recipe-subprocess. Here is what the edit looks like:

def rcmd_in(server):
    import subprocess as sp
    import os, os.path
    si = sp.STARTUPINFO()
    si.dwFlags |= sp.STARTF_USESHOWWINDOW
    for i in command_list:
        result = sp.run(['C:/"Path to executable"/rcmd.exe', '\\\\' + server, i],
                       universal_newlines=True, stdin=sp.PIPE, stdout=sp.PIPE, 
                       stderr=sp.STDOUT, startupinfo=si, env=os.environ)
        print(result.stdout)

我现在遇到的问题是当它运行错误时会弹出错误:8 - 内部错误 -109".让我补充一下,我尝试使用函数call()"、Popen()"等,但似乎只有run()"有效.

The the problem I have now is when it runs an error of "Error:8 - Internal error -109" pops up. Let me add I tried using functions 'call()', 'Popen()', and others but only 'run()' seems to work.

我已经到了大脑受伤的地步,我需要一些帮助.有什么建议?和往常一样,我永远乐于帮助任何人.提前致谢!

I've reached a point where my brain hurts and I can use some help. Any suggestions? As always I am forever great full for anyone's help. Thanks in advance!

推荐答案

我想通了,只用了 5 天!:D

I figured it out and it only took me 5 days! :D

看起来函数失败的原因在于 Windows 如何处理标准输入.我发现了一个 post 帮助我编辑了我的代码以使用 pyinstaller -w (--noconsole).这是更新后的代码:

Looks like the reason the function would fail falls on how Windows handles stdin. I found a post that helped me edit my code to work with pyinstaller -w (--noconsole). Here is the updated code:

def rcmd_in(server):
    import subprocess as sp
    si = sp.STARTUPINFO()
    si.dwFlags |= sp.STARTF_USESHOWWINDOW
    for i in command_list:
        result = sp.Popen(['C:/"Path to executable"/rcmd.exe', '\\\\' + server, i],
                       universal_newlines=True, stdin=sp.PIPE, stdout=sp.PIPE, 
                       stderr=sp.PIPE, startupinfo=si)
        print(result.stdout.read())

注意函数 'run()' 到 'Popen()' 的变化.'run()' 函数将不会与最后的打印语句一起使用.此外,对于那些好奇的人,我创建的si"变量阻止了子进程"在使用 GUI 运行时打开控制台.我希望这对为此苦苦挣扎的人有用.干杯

Note the change of functions 'run()' to 'Popen()'. The 'run()' function will not work with the print statement at the end. Also, for those of you who are curious the 'si' variable I created is preventing 'subprocess' from opening a console when being ran while using a GUI. I hope this will become useful to someone struggling with this. Cheers

这篇关于在 Windows 10 上使用 tkinter 在 GUI 的后台运行控制台窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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