【问题标题】:Python TKinter redirect terminal output to GUI windowPython TKinter 将终端输出重定向到 GUI 窗口
【发布时间】:2018-10-16 20:46:15
【问题描述】:

我是 python 新手,正在尝试创建一个程序,除其他外,该程序应该将终端输出打印到 Tkinter GUI。

例如 ping 命令或任何其他 shell 命令或变量 print 的输出。

我在网上搜索了解决方案,但没有找到任何简单的方法。

也许我遗漏了什么,或者有其他方法可以在 python 中打印终端输出

提前致谢,

【问题讨论】:

标签: python python-3.x shell tkinter


【解决方案1】:

您可以使用 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()

【讨论】:

  • 如果你安装了 pycharm IDE,它会提示你应该修改哪个错误。
猜你喜欢
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
  • 1970-01-01
  • 2017-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多