【问题标题】:How to make your OWN console like windows command prompt如何使您的 OWN 控制台像 Windows 命令提示符
【发布时间】:2018-05-28 09:41:27
【问题描述】:

正如您在 PyCharm 中看到的, 最先进的控制台,我正在尝试制作一个 请告诉我怎么做。

我知道 subprocess 模块在这种情况下非常方便。

我有一个名为 add.exe 的 exe 文件 该 add.exe 文件的 Python 3x 代码将是,

a = input('Enter 1st Number')
b = input('Enter 2nd Number')
print('a + b =', a + b)

现在,当我使用子进程在后台运行它并获取输出然后提供输入时,我只得到一个黑色的大空白控制台屏幕。 哦!这看起来很丑。

我只想获取我的输出并在程序需要输入时得到提示,但是 无需打开控制台 我的代码到目前为止是这样的,

from subprocess import Popen, PIPE
p = Popen(['add.exe'],
          stdout=PIPE,
          stdin=PIPE,
          )
p.stdin.write(b'4')
p.stdin.write(b'6')
print(p.stdout.read())

然后我得到那个愚蠢的控制台 当我关闭那个控制台时,我会在我的 IDLE 上得到输出,

b'输入第一个数字:'

我该怎么办!! 请一些人帮忙。

【问题讨论】:

    标签: python python-3.x subprocess pycharm pyqt5


    【解决方案1】:

    控制台.py

    #!/usr/bin/env python3
    
    from subprocess import check_output, PIPE
    
    commands = ['add.exe', 'sub.exe', 'print', 'exit', 'quit']
    
    # Only commands with a set number of arguments
    num_cmd_args = {
        'add.exe': 2,
        'sub.exe': 2
    }
    
    # Command help messages
    cmd_helps = {
        'add.exe': 'Adds 2 numbers together. Returns num1 + num2',
        'sub.exe': 'Subtracts 2 numbers. Returns num1 - num2',
        'print': 'Prints all arguments passed'
    }
    
    while True:
        user_in = input('$ ').split()
        cmd = user_in[0]
        args = user_in[1:]
        # Default to '*' if not cmd not found in num_cmd_args
        n_args_needed = num_cmd_args.get(cmd, '*')
    
        # Check cmd
        if cmd not in commands:
            print('command not found:', cmd)
            continue
        elif cmd in ('exit', 'quit'):
            break
    
        # To make this much better, you're probably going to want to
        #  do some type checking to make sure that the user entered
        #  numbers for commands like add.exe and sub.exe. I suggest
        #  also looking at word splitting and maybe escaping special
        #  characters like \x07b or \u252c.
    
        # Check cmd's args
        if n_args_needed != len(args):
            print(cmd_helps[cmd])
        elif n_args_needed in ('*', num_cmd_args[cmd]):
            out = check_output(['./'+cmd, *args])
            print(out.decode(), end='')
    

    这些是我的 c++ 文件:


    添加.cpp

    #include <iostream>
    using namespace std;
    
    int main(int argc, char** argv) {
        cout << atoi(argv[1]) + atoi(argv[2]) << endl;
        return 0;
    }
    


    sub.cpp

    #include <iostream>
    using namespace std;
    
    int main(int argc, char** argv) {
        cout << atoi(argv[1]) - atoi(argv[2]) << endl;
        return 0;
    }
    


    打印.cpp

    #include <iostream>
    using namespace std;
    
    int main(int argc, char** argv) {
        for (int i=1; i<argc; i++)
            cout << argv[i] << endl;
        return 0;
    }
    

    【讨论】:

    • 兄弟,我明白了,但正如您所见,我正在尝试创建另一个类似于 pycharm 的控制台,因此可执行文件可能包含不同类型的输入或输出消息,所以请您修改程序以便它可以废弃控制台消息并在需要时提示我输入,然后再次显示结果输出。
    • @Nikhil 就是这样,除非程序有帮助消息选项(例如,add.exe /?),否则无法知道程序需要什么样的输入。即使是这样,您也必须进行一些相当复杂的解析才能了解帮助消息告诉您程序需要什么样的输入。对于您想要的,您必须编写逻辑代码,以便能够处理用户可能尝试运行的每个程序的输入。
    • 如果我把这段代码放在 while 循环中会怎样p.stdout.read(1) 如果我什至这样做,那么一旦光标在控制台中到达结束字符就会出现一个问题,然后程序将永远运行,除非我提供输入但问题是它挂起,然后无法使用此代码提供任何类型的输入print('some input', file=p.stdin)请帮助...
    • 当我说我的代码将挂起时,我的意思是代码将等待,除非程序退出或者我提供了所有需要的输入,这意味着它正在等待 EOF。
    • @Nikhil 我可以让可执行文件读取任何类型的输入的唯一方法是通过 Popen 调用传递它,就像我在我的示例中那样。如果您可以使用p.stdinprint(..., file=p.stdin) 使其工作,那很棒,但您仍然需要知道程序需要什么样的输入并将该逻辑编码到您的术语模拟器模拟器中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2014-02-21
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多