【发布时间】:2015-10-06 21:49:47
【问题描述】:
我想在我的网络中每秒 ping 一个特定的主机名,并更改相应按钮的名称。 现在我有这个:
import tkinter as tk
import time
# variables
hostname = ("ASUS-PC")
mot= "inactif"
class test(tk.Tk):
# make buttons on grid
def __init__(self):
tk.Tk.__init__(self)
self.button = list()
for i in range(10):
i=i+1
RN = ('RN '+str(i))
self.button.append(tk.Button(text=RN))
self.button[-1].grid(row=0,column=i)
# ping hostname every second
def ping(self):
import subprocess
import os
result=subprocess.Popen(["ping", "-n", "1", "-w", "200", hostname],shell=True).wait()
if result == 0:
print (hostname, "active")
else:
B = self.button[0]
B ['text'] = mot
time.sleep(1)
while True:
ping()
app = test ()
app.mainloop()
它不起作用,我不知道为什么。一开始这是一个“自我”问题,但现在它似乎与我每秒 ping 的方式有关(我从那里得到它 What is the best way to repeatedly execute a function every x seconds in Python?)如果有人知道答案......
感谢您的帮助
【问题讨论】:
-
变量
button被引用时超出范围。相关:stackoverflow.com/questions/291978/… -
你的意思是说
self.button(注意你甚至错过了ping上的适当参数)?函数中的局部变量无法从外部访问,故意。