版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gamers/article/details/82118921
就是这样子的了。。没办法。我抽时间再修改。
8H的working
#!/use/bin/python
#base python3 code
#-*-coding:utf-8-*-
from tkinter import *
import time, urllib.request
import threading
import tkinter.messagebox
# GUI窗口类
class Control:
# 定义GUI界面
def __init__(self, master, fuc):
self.parent = master
self.fuc = fuc
self.parent.title("网页定时监控-1.0")
self.parent.geometry("500x650")
self.parent.resizable(0, 0)
self.网页监控 = Label(self.parent, text="请输入网页地址:")
self.网页监控.grid(row=0, column=0)
self.监控时间间隔 = Label(self.parent, text="监控时间间隔(s):")
self.监控时间间隔.grid(row=1, column=0)
self.string1 = StringVar()
self.string1.set("http://124.239.146.214:88/")
self.textfiled1 = Entry(self.parent, width =50, textvariable=self.string1)
self.textfiled1.grid(row=0, column=1)
self.string2 = StringVar()
self.string2.set("5")
self.监控时间 = Entry(self.parent, width=50, textvariable=self.string2)
self.监控时间.grid(row=1, column=1)
self.运行 = Button(self.parent, text="运行", command=self.fuc)
self.运行.grid(row=4, column=0)
self.停止 = Button(self.parent, text="停止中", state="disable", command=self.monitor_stop)
self.停止.grid(row=4, column=1)
self.退出 = Button(self.parent, text="退出", command=self.win_quit)
self.退出.grid(row=4, column=2)
self.tile = Label(self.parent, text="监控信息:")
self.tile.grid(row=5, column=0)
# self.string3 = StringVar()
self.txinfo = Text(self.parent, height=40, width=50)
self.txinfo.grid(row=6, columnspan=3)
#定义停止按钮的功能
def monitor_stop(self):
global monitor_status,stopTime
stopTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
monitor_status = False
self.运行["text"] = "运行"
self.运行["state"] = "normal"
self.停止["state"] = "disable"
self.停止["text"] = "停止中"
self.监控时间["state"] = "normal"
self.textfiled1["state"]="normal"
final_info = "从%s到%s,监控的%s正常,\n\n不正常次数%d"%(startTime, stopTime, monitor_url, num)
tkinter.messagebox.showinfo("提示", final_info)
#定义退出按钮的功能
def win_quit(self):
# global startTime
if self.运行["text"] == "运行中":
final_info = "从%s到%s,监控的%s正常,\n\n不正常次数%d"%(startTime, stopTime, monitor_url, num)
tkinter.messagebox.showinfo("提示", final_info)
self.parent.destroy()
#定义选项功能:
def select_fuc(self):
pass
# 具体功能类
class ThreadRoot:
def __init__(self, master):
self.master = master
self.gui = Control(self.master, self.starting) # 将我们定义的GUI类赋给服务类的属性,将执行的功能函数作为参数传入
# 监听方法
def monitor_web(self):
global monitor_status,startTime,monitor_url,num
startTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
monitor_status = True
monitor_url = self.gui.textfiled1.get()
slep_time = self.gui.监控时间.get()
slep_time = int(slep_time)
self.gui.运行["text"] = "运行中"
self.gui.运行["state"] = "disable"
self.gui.停止["state"] = "normal"
self.gui.停止["text"] = "停止"
self.gui.监控时间["state"] = "disable"
self.gui.textfiled1["state"]="disable"
if "http" not in monitor_url:
monitor_url = "http://" + monitor_url
while monitor_status:
try:
usrlSizecontent = urllib.request.urlopen(monitor_url, timeout= slep_time)
except:
num +=1
time.sleep(slep_time)
continue
if usrlSizecontent.code == 200:
if monitor_status:
succ_info = "%s正常\n"%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print("%s"%(succ_info))
self.gui.txinfo.insert(END,succ_info)
else:
fail_info = "%s不正常\n"%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print("%s"%(fail_info))
self.gui.txinfo.insert(END, fail_info)
num +=1
time.sleep(slep_time)
# print("monitor_url=%s,正常"%(monitor_url))
# 为方法开一个单独的线程
def starting(self):
self.thread = threading.Thread(target=self.monitor_web)
self.thread.setDaemon(True) #线程守护,即主进程结束后,此线程也结束。否则主进程结束子进程不结束
self.thread.start()
if __name__ == '__main__':
startTime = ""
stopTime = ""
monitor_url = ""
num = 0
monitor_status = False #子线程循环标识位
root = Tk()
tool = ThreadRoot(root)
root.mainloop()
使用线程