[英]Show command prompt in tkinter window
How do I make a program where I can run a piece of code, then show the results?如何制作一个可以运行一段代码的程序,然后显示结果? So if I make my program run
python --version
it should print something like Python 3.8.3 (depends on what version you are on), but you get the point因此,如果我让我的程序运行
python --version
它应该打印类似 Python 3.8.3 的内容(取决于您使用的版本),但您明白了
PS: I know this has been posted before, but they don't work for me:( PS:我知道这已经发布过,但它们对我不起作用:(
Thanks!!谢谢!!
So here I made a very simple version of what You may want (type in python --version
to try out):所以在这里我做了一个你可能想要的非常简单的版本(输入
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()
the subprocess.getoutput()
gets the output the cmd would give if the given command was used subprocess.getoutput
subprocess.getoutput()
获取 output 如果使用给定命令,cmd 将给出
EDIT (moved comment here):编辑(在此处移动评论):
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 process and then puts it all out (maybe for that it is possible to use threads to not make the window unresponsive at least) 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 无响应)
EDIT (28.07.2021.):编辑(2021 年 7 月 28 日):
Probably better to use subprocess.Popen
and stream data from there从那里使用
subprocess.Popen
和 stream 数据可能更好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.