[英]Python TKinter redirect terminal output to GUI window
我是 Python 新手,正在尝试创建一个程序,该程序应该将终端输出打印到 Tkinter GUI。
例如 ping 命令或任何其他 shell 命令或变量打印的输出。
我在网上搜索了解决方案,但没有找到任何简单的方法。
也许我遗漏了一些东西,或者有其他方法可以在 python 中打印终端输出
提前致谢,
您可以使用 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.