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的
workerthread
,从中收集结果并将它们放入一个Queue
,其内容定期被传输到GUI。在它使用
time.sleep()
,因为它是在一个单独的线程中完成的,而不是使用tkinter,所以它没问题。在请注意,您可能希望在GUI中添加一个垂直滚动条。在
相关问题 更多 >
编程相关推荐