python - 将终端输出重定向到 tkinter

标签 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 中?

最佳答案

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

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

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

    # 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)
        #self.widget.see('end') # autoscroll

    #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

两个问题:

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

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


编辑:

带有线程 的版本。它解决了以前的问题。

但它可能会有新的问题:)

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

  • 代码没有停止线程的方法(即当您运行 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)
        #self.widget.see('end') # autoscroll

    #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

编辑:2021.08.19

带有 Scrollbarautoscroll 的版本

import tkinter as tk
import sys
import subprocess
import threading 

# --- classes ---

class Redirect():

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

    def write(self, text):
        self.widget.insert('end', text)
        if self.autoscroll:
            self.widget.see("end")  # autoscroll
        
    #def flush(self):
    #    pass

# --- functions ---

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

def test():
    print("Thread: start")

    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("Thread: end")

# --- main ---    

root = tk.Tk()

# - Frame with Text and Scrollbar -

frame = tk.Frame(root)
frame.pack(expand=True, fill='both')

text = tk.Text(frame)
text.pack(side='left', fill='both', expand=True)

scrollbar = tk.Scrollbar(frame)
scrollbar.pack(side='right', fill='y')

text['yscrollcommand'] = scrollbar.set
scrollbar['command'] = text.yview

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

# - rest -

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

root.mainloop()

# - after close window -

sys.stdout = old_stdout

关于python - 将终端输出重定向到 tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62335989/

相关文章:

linux - 如何将 bash 输出写入任意程序标准输入

python - 在 Python 中捕获 PyCharm 停止信号

java - 如何在Java中使用ElasticSearch JSON DSL?

python - 如何将更新后的页面内容传递给另一个函数?

macos - 该应用程序无法启动,因为它无法找到或加载Qt平台插件 "cocoa"

php - CodeIgniter + mod_rewrite url重写错误

python - 从 pip 下载时排除 manylinux wheels

Python 和 Google Places API |想要获取特定位置的所有 Restaurants

python - 检查 Python 中的无效路径

linux - Wine 升级到版本 1.4.1 后 DLL 文件丢失