将终端输出重定向到tkinter

时间:2020-06-12 01:14:40

标签: python python-3.x linux tkinter

我正在尝试为我的程序制作一个非常简单的GUI(在这里,我已将其替换为测试功能)。我可以通过gui运行我的代码,但是程序的输出正在终端中显示。我希望输出在我的原始窗口中可见。

这是我的代码:

import tkinter as tk
from test import *
from tkinter import *
import os

root = tk.Tk()

canvas = tk.Canvas(root, height=400, width =550, bg ="#202020") 
canvas.pack()

#frame = tk.Frame(root, bg="white");
#frame.place(relwidth=0.8, relheight = 0.8, relx=0.1, rely=0.1)

topFrame = tk.Frame(root, bg="#202020")
topFrame.place(relwidth=1, relheight = 0.75)

bottomFrame = tk.Frame(root, bg="#202020")
bottomFrame.place(rely=0.75, relwidth=1, relheight = 0.25)


launch = tk.Button(bottomFrame, text="Launch", bg="white", fg="#202020", font="noah 10 bold", padx=20, command=test)
launch.place(in_=bottomFrame, rely=0.5, relx=0.5, anchor=CENTER)

root.mainloop()

这是我从中调用该函数的另一个文件:

import os
def test():
    print("Hello World")
    os.system("ping 192.168.0.1 -c 4")

是否可以捕获此函数的输出并将其实时显示在gui的 topframe 中?

1 个答案:

答案 0 :(得分:1)

您可以使用函数write()创建类,该类将文本插入tkinter.Text并将其实例分配给sys.stdout-然后print()将发送给tkinter.Text < / p>

class Redirect():

    def __init__(self, widget):
        self.widget = widget

    def write(self, text):
        self.widget.insert('end', text)

    # some widget may need it
    #def flush(self):
    #    pass

text = tk.Text(root)
text.pack()

# keep original stdout
old_stdout = sys.stdout    

# assing Redirect with widget Text 
sys.stdout = Redirect(text)

root.mainloop()

# assign back original stdout (if you need it)
sys.stdout = old_stdout

但是os.system必须替换为ie。 subprocess.run()捕获输出并print()

def test():
    print("Hello World")
    p = subprocess.run("ping -c 4 stackoverflow.com", shell=True, stdout=subprocess.PIPE)
    print(p.stdout.decode())

最小工作代码

import tkinter as tk
import os
import sys
import subprocess

# --- functions ---

def test():
    print("Hello World")
    p = subprocess.run("ping -c 4 stackoverflow.com", shell=True, stdout=subprocess.PIPE)
    print(p.stdout.decode())

# --- classes ---

class Redirect():

    def __init__(self, widget):
        self.widget = widget

    def write(self, text):
        self.widget.insert('end', text)

    #def flush(self):
    #    pass

# --- main ---    

root = tk.Tk()

text = tk.Text(root)
text.pack()

button = tk.Button(root, text='TEST', command=test)
button.pack()

old_stdout = sys.stdout    
sys.stdout = Redirect(text)

root.mainloop()

sys.stdout = old_stdout

两个问题:

subprocess获取所有文本后,它将文本从Text发送到小部件ping。逐行显示可能需要更多的工作。

test()需要一些时间才能完成工作,并且它阻塞了tkinter,因此它无法更新小部件并且冻结。它可能需要在单独的test()中运行thread,但是我不知道这是否不会带来其他问题,因为在许多GUI框架中,您不能在单独的威胁中使用小部件。


编辑:带有threading的版本。它解决了以前的问题。

但是它可能有新问题:)

  • 您可以单击两次按钮,它将同时运行两个pings并混合来自两个pings的字符串

  • 代码没有停止线程的方法(即,当您运行ping而没有-c 4时)

代码:

import tkinter as tk
import sys
import subprocess
import threading 

# --- functions ---

def run():
    threading.Thread(target=test).start()

def test():
    print("Hello World")

    p = subprocess.Popen("ping -c 4 stackoverflow.com".split(), stdout=subprocess.PIPE, bufsize=1, text=True)
    while p.poll() is None:
        msg = p.stdout.readline().strip() # read a line from the process output
        if msg:
            print(msg)

    print("Finished")

# --- classes ---

class Redirect():

    def __init__(self, widget):
        self.widget = widget

    def write(self, text):
        self.widget.insert('end', text)

    #def flush(self):
    #    pass

# --- main ---    

root = tk.Tk()

text = tk.Text(root)
text.pack()

button = tk.Button(root, text='TEST', command=run)
button.pack()

old_stdout = sys.stdout    
sys.stdout = Redirect(text)

root.mainloop()

sys.stdout = old_stdout
相关问题