Py学习  »  Python

Python Tkinter输出文本到文本小部件

RLynch • 5 年前 • 1770 次点击  
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()

我在这里写了一段代码,它将发送一个ping到一个IP地址并返回一个结果。它在CLI上运行良好,但是我希望将它“打印”到 Text 使用Tkinter的小部件。我正处在它会把它送到 文本 但是它只在我中断程序后才使自己可见。我想在ping通过循环进行时,将滚动输出到GUI文本区域。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49876
 
1770 次点击  
文章 [ 1 ]  |  最新文章 5 年前