简体   繁体   中英

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.

For example the output of a ping command or any other shell command or a variable print.

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

Thanks in advance,

You can use subprocess module and Canvas widget to print the result of shell commands to the screen

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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