如何将子流程文本输出发送到Tkinter文本小部件? [英] How to send subprocess text output to a Tkinter text widget?

查看:173
本文介绍了如何将子流程文本输出发送到Tkinter文本小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import subprocess
import os 
import time
from tkinter import *
root=Tk()
textbox=Text(root)
textbox.pack()


def redirector(inputStr):
    textbox.insert(INSERT, inputStr)

def address_ping():
        '''
        DOCSTRING -> Ip addresses in servers.txt are
                     192.168.0.1     192.168.0.26
        '''
        while True:
            with open('servers.txt', 'r') as f:
                for ip in f:
                    result=subprocess.Popen(["ping", "-c", "7", "-n", "-W", "2", ip],stdout=f, stderr=f).wait()
                    if result:
                        print("ip address " + ip, "is inactive")
                        sys.stdout.write = redirector

                    else:
                        print("ip address " + ip, "is active")
                        sys.stdout.write = redirector
                    pass    

address_ping()        

root.mainloop()

我在这里编写了一段代码,它将向IP地址发送ping并返回结果.它在CLI上运行良好,但是我希望使用Tkinter将其打印"到Text小部件中.我正好将其发送到Text小部件GUI,但只有在我中断程序后,它才会显示出来.我希望在ping通过循环进行时向GUI文本区域提供滚动输出.

解决方案

此处使用多线程的东西似乎可以满足您的要求.主程序分为处理QUI的部分和管理ping subprocess的单独的workerthread,从中收集结果并将它们放在Queue中,其内容定期传送到GUI.

它使用time.sleep()是因为它是在单独的线程中完成的,而该线程使用tkinter 不是,所以可以.

注意,您似乎可能想在GUI中添加垂直滚动条.

import subprocess
import queue
import threading
import time
import tkinter as tk


class GuiPart:
    def __init__(self, master, queue, end_command):
        self.queue = queue
        self.master = master
        self.textbox = tk.Text(root)
        self.textbox.pack()
        btn = tk.Button(master, text='Quit', command=end_command)
        btn.pack(expand=True)

    def process_incoming(self):
        """ Handle all messages currently in the queue. """
        while self.queue.qsize():
            try:
                info = self.queue.get_nowait()
                self.textbox.insert(tk.INSERT, info)
            except queue.Empty:  # Shouldn't happen.
                pass


class ThreadedClient:
    """ Launch the main part of the GUI and the worker thread.
        periodic_call() and end_application() could reside in the GUI part, but
        putting them here keeps all the thread controls in a single place.
    """
    def __init__(self, master):
        self.master = master
        self.queue = queue.Queue()

        # Set up the GUI part.
        self.gui = GuiPart(master, self.queue, self.end_application)

        # Set up the background processing thread.
        self.running = True
        self.thread = threading.Thread(target=self.workerthread)
        self.thread.start()

        # Start periodic checking of the queue.
        self.periodic_call(200)

    def periodic_call(self, delay):
        """ Every delay ms process everything new in the queue. """
        self.gui.process_incoming()
        if not self.running:
            sys.exit(1)
        self.master.after(delay, self.periodic_call, delay)

    # Runs in separate thread - NO tkinter calls allowed.
    def workerthread(self):
        while self.running:
            with open('servers.txt', 'r') as file:
                for ip in file:
                    rc = subprocess.Popen(["ping", "-c", "7", "-n", "-W", "2", ip]).wait()
                    if rc:
                        self.queue.put('ip address {} is inactive\n'.format(ip))
                    time.sleep(1)

    def end_application(self):
        self.running = False  # Stop queue checking.
        self.master.quit()


if __name__ == '__main__':
    root = tk.Tk()
    root.title('Pinger')
    client = ThreadedClient(root)
    root.mainloop()  # Display application window and start tkinter event loop.

import subprocess
import os 
import time
from tkinter import *
root=Tk()
textbox=Text(root)
textbox.pack()


def redirector(inputStr):
    textbox.insert(INSERT, inputStr)

def address_ping():
        '''
        DOCSTRING -> Ip addresses in servers.txt are
                     192.168.0.1     192.168.0.26
        '''
        while True:
            with open('servers.txt', 'r') as f:
                for ip in f:
                    result=subprocess.Popen(["ping", "-c", "7", "-n", "-W", "2", ip],stdout=f, stderr=f).wait()
                    if result:
                        print("ip address " + ip, "is inactive")
                        sys.stdout.write = redirector

                    else:
                        print("ip address " + ip, "is active")
                        sys.stdout.write = redirector
                    pass    

address_ping()        

root.mainloop()

I am writing a piece of code here that will send a ping to an IP address and return a result. It works fine on the CLI, however I wish to "print" it to a Text widget using Tkinter. I am at a point where it will send it to the Text widget GUI but it only makes itself visible after I interrupt the program. I want to have a rolling output to the GUI text area as the pings progress through a loop.

解决方案

Here's something that uses multithreading that seems to do what you want. The main program is split into a portion that handles the QUI and a separate workerthread that manages the ping subprocess, collecting the results from it and putting them in a Queue whose contents periodically get transferred to the GUI.

It uses time.sleep() because it's done in a separate thread that's not using tkinter so it's OK.

Note, seems likely you'll might want to add a vertical scrollbar to the GUI.

import subprocess
import queue
import threading
import time
import tkinter as tk


class GuiPart:
    def __init__(self, master, queue, end_command):
        self.queue = queue
        self.master = master
        self.textbox = tk.Text(root)
        self.textbox.pack()
        btn = tk.Button(master, text='Quit', command=end_command)
        btn.pack(expand=True)

    def process_incoming(self):
        """ Handle all messages currently in the queue. """
        while self.queue.qsize():
            try:
                info = self.queue.get_nowait()
                self.textbox.insert(tk.INSERT, info)
            except queue.Empty:  # Shouldn't happen.
                pass


class ThreadedClient:
    """ Launch the main part of the GUI and the worker thread.
        periodic_call() and end_application() could reside in the GUI part, but
        putting them here keeps all the thread controls in a single place.
    """
    def __init__(self, master):
        self.master = master
        self.queue = queue.Queue()

        # Set up the GUI part.
        self.gui = GuiPart(master, self.queue, self.end_application)

        # Set up the background processing thread.
        self.running = True
        self.thread = threading.Thread(target=self.workerthread)
        self.thread.start()

        # Start periodic checking of the queue.
        self.periodic_call(200)

    def periodic_call(self, delay):
        """ Every delay ms process everything new in the queue. """
        self.gui.process_incoming()
        if not self.running:
            sys.exit(1)
        self.master.after(delay, self.periodic_call, delay)

    # Runs in separate thread - NO tkinter calls allowed.
    def workerthread(self):
        while self.running:
            with open('servers.txt', 'r') as file:
                for ip in file:
                    rc = subprocess.Popen(["ping", "-c", "7", "-n", "-W", "2", ip]).wait()
                    if rc:
                        self.queue.put('ip address {} is inactive\n'.format(ip))
                    time.sleep(1)

    def end_application(self):
        self.running = False  # Stop queue checking.
        self.master.quit()


if __name__ == '__main__':
    root = tk.Tk()
    root.title('Pinger')
    client = ThreadedClient(root)
    root.mainloop()  # Display application window and start tkinter event loop.

这篇关于如何将子流程文本输出发送到Tkinter文本小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆