python 中带有 GUI 的实时互联网连接检查器
Real Time internet connection checker in python With GUI
所以基本上我想做的是检查计算机是否可以访问互联网直到程序结束....
它在一个用 tkinter 制作的 GUI 中.....我试图创建新线程和 运行 while 循环中的函数(while 1:),但它说
Traceback (most recent call last):
.
.
.
RuntimeError: main thread is not in main loop
这是程序
import threading
import socket
import time
def is_connected():
try:
socket.create_connection(("www.google.com", 80))
print("Online",end="\n")
except OSError:
print("offline",end="\n")
tt3 =threading.Event()
while 1:
t3=threading.Thread(target=is_connected)
t3.start()
time.sleep(1)
这是带有 GUI 的程序
import threading
import socket
import time
import tkinter
top = tkinter.Tk()
top.title("")
l=tkinter.Label(top,text='')
l.pack()
def is_connected():
try:
socket.create_connection(("www.google.com", 80))
print("Online",end="\n")
l.config(text="Online")
except OSError:
l.config(text="offline")
print("offline",end="\n")
tt3 =threading.Event()
while 1:
t3=threading.Thread(target=is_connected)
t3.start()
time.sleep(1)
top.configure(background="#006666")
top.update()
top.mainloop()
欢迎任何建议或帮助!!
(reddit 上有人建议我使用我不知道的队列)
首先,while 循环会阻止 tkinter 主循环处理事件。其次,您在每个循环中重复创建新线程。
更好地使用.after()
:
import socket
import tkinter
top = tkinter.Tk()
top.title("Network Checker")
top.configure(background="#006666")
l=tkinter.Label(top,text='Checking ...')
l.pack()
def is_connected():
try:
socket.create_connection(("www.google.com", 80)) # better to set timeout as well
state = "Online"
except OSError:
state = "Offline"
l.config(text=state)
print(state)
top.after(1000, is_connected) # do checking again one second later
is_connected() # start the checking
top.mainloop()
所以基本上我想做的是检查计算机是否可以访问互联网直到程序结束....
它在一个用 tkinter 制作的 GUI 中.....我试图创建新线程和 运行 while 循环中的函数(while 1:),但它说
Traceback (most recent call last):
.
.
.
RuntimeError: main thread is not in main loop
这是程序
import threading
import socket
import time
def is_connected():
try:
socket.create_connection(("www.google.com", 80))
print("Online",end="\n")
except OSError:
print("offline",end="\n")
tt3 =threading.Event()
while 1:
t3=threading.Thread(target=is_connected)
t3.start()
time.sleep(1)
这是带有 GUI 的程序
import threading
import socket
import time
import tkinter
top = tkinter.Tk()
top.title("")
l=tkinter.Label(top,text='')
l.pack()
def is_connected():
try:
socket.create_connection(("www.google.com", 80))
print("Online",end="\n")
l.config(text="Online")
except OSError:
l.config(text="offline")
print("offline",end="\n")
tt3 =threading.Event()
while 1:
t3=threading.Thread(target=is_connected)
t3.start()
time.sleep(1)
top.configure(background="#006666")
top.update()
top.mainloop()
欢迎任何建议或帮助!! (reddit 上有人建议我使用我不知道的队列)
首先,while 循环会阻止 tkinter 主循环处理事件。其次,您在每个循环中重复创建新线程。
更好地使用.after()
:
import socket
import tkinter
top = tkinter.Tk()
top.title("Network Checker")
top.configure(background="#006666")
l=tkinter.Label(top,text='Checking ...')
l.pack()
def is_connected():
try:
socket.create_connection(("www.google.com", 80)) # better to set timeout as well
state = "Online"
except OSError:
state = "Offline"
l.config(text=state)
print(state)
top.after(1000, is_connected) # do checking again one second later
is_connected() # start the checking
top.mainloop()