这里有一些东西,使用多线程,似乎做你想要的。主程序分为处理QUI的部分和单独的
workerthread
管理ping的
subprocess
,从中收集结果并将其放入
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.