繁体   English   中英

在 tkinter window 中显示命令提示符

[英]Show command prompt in tkinter window

如何制作一个可以运行一段代码的程序,然后显示结果? 因此,如果我让我的程序运行python --version它应该打印类似 Python 3.8.3 的内容(取决于您使用的版本),但您明白了

PS:我知道这已经发布过,但它们对我不起作用:(

谢谢!!

所以在这里我做了一个你可能想要的非常简单的版本(输入python --version试试):

from tkinter import Tk, Text
import subprocess


def run(event):
    command = cmd.get('1.0', 'end').split('\n')[-2]
    if command == 'exit':
        exit()
    cmd.insert('end', f'\n{subprocess.getoutput(command)}')


root = Tk()

cmd = Text(root)
cmd.pack()

cmd.bind('<Return>', run)

root.mainloop()

subprocess.getoutput subprocess.getoutput()获取 output 如果使用给定命令,cmd 将给出

编辑(在此处移动评论):
there are some limitations however for example running pause will just crash tkinter and the output will be given only after command has finished running for example if You tracert google.com it may take a while and during that the window will be unresponsive until it completes the处理,然后将其全部输出(也许可以使用线程至少不使 window 无响应)

编辑(2021 年 7 月 28 日):
从那里使用subprocess.Popen和 stream 数据可能更好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM