简体   繁体   English

Python TKinter 将终端输出重定向到 GUI 窗口

[英]Python TKinter redirect terminal output to GUI window

I'm new in python and trying to create a program which should, among other things, to print a terminal output to a Tkinter GUI.我是 Python 新手,正在尝试创建一个程序,该程序应该将终端输出打印到 Tkinter GUI。

For example the output of a ping command or any other shell command or a variable print.例如 ping 命令或任何其他 shell 命令或变量打印的输出。

I've searched the web for a solution but did not find any simple way to do so.我在网上搜索了解决方案,但没有找到任何简单的方法。

Maybe I'm missing something or there is other way to print the terminal output in python也许我遗漏了一些东西,或者有其他方法可以在 python 中打印终端输出

Thanks in advance,提前致谢,

You can use subprocess module and Canvas widget to print the result of shell commands to the screen您可以使用 subprocess 模块和 Canvas 小部件将 shell 命令的结果打印到屏幕上

from tkinter import *
import subprocess


root = Tk()
frame = Frame(root,width=1024, height=768)
frame.grid(row=0, column=0)
c = Canvas(frame, bg='blue', width=800, height=600)
c.config(scrollregion=(0, 0, 800, 3000))
sbar = Scrollbar(frame)
sbar.config(command=c.yview)
c.config(yscrollcommand=sbar.set)
sbar.pack(side=RIGHT, fill=Y)
c.pack(side=LEFT, expand=True, fill=BOTH)

String1 = subprocess.check_output('chcp 437 && ping /?', shell=True)
c.create_text(400, 0, anchor=N, fill='orange', font='Times 15', text=String1)
# c.create_text(750, 300, anchor=W, fill='orange', font='Times 28', text='List')

button = Button(root, text="Quit", command=root.destroy)
c.create_window(400, 0, anchor=N, window=button)

mainloop()

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

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