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 上运行良好,但我希望Text
使用 Tkinter 将其“打印”到小部件。我正处于将其发送到小部件 GUI 的地步,Text
但它仅在我中断程序后才使自己可见。当 ping 通过循环进行时,我希望滚动输出到 GUI 文本区域。