I wanted to write a code that will check the availability of sites using "status_code" But in the end I got into a stupor, I do not know how to implement the verification of each site entered into the widgets I manage to check only one site from one widget, but I need to check each one, and set a time for each site to check I would like to know, or at least get hints on how to implement it I will be grateful for any help
My attempt:
import tkinter as tk
from tkinter import ttk
import requests
import time
from tkinter import *
from tkinter import messagebox
window = Tk()
window.geometry('400x700')
window.title("SiteChecker")
def SiteCheck():
res=int(tim1.get())
Site_Value = txt1.get()
Get_Response = requests.get(Site_Value)
time.sleep(res)
if Get_Response.status_code != 200:
#as I understand it, you need to make a "for" loop, but I don't understand how to implement
def clicked():
txt = Entry(window, width=18)
txt.grid(column=0, pady=8)
txt_row = txt.grid_info()['row']
tim = Entry(window, width=3)
tim.grid(row=txt_row, column=1, pady=8)
lbl1 = Label(window, text="Enter references:")
lbl1.grid(column=0, row=1)
lbl2 = Label(window, text="Enter the test time: ")
lbl2.grid(column=1, row=1)
lbl3 = Label(window, text="Availability status ")
lbl3.grid(column=2, row=1)
txt1 = Entry(window,width=18)
txt1.grid(column=0, row=2, pady=8)
txt2 = Entry(window,width=18)
txt2.grid(column=0, row=3,pady=8)
txt3 = Entry(window,width=18)
txt3.grid(column=0, row=4,pady=8)
txt4 = Entry(window,width=18)
txt4.grid(column=0, row=5,pady=8)
txt5 = Entry(window,width=18)
txt5.grid(column=0, row=6,pady=8)
tim1 = Entry(window,width=3)
tim1.grid(column=1, row=2, pady=8)
tim2 = Entry(window,width=3)
tim2.grid(column=1, row=3, pady=8)
tim3 = Entry(window,width=3)
tim3.grid(column=1, row=4, pady=8)
tim4 = Entry(window,width=3)
tim4.grid(column=1, row=5, pady=8)
tim5 = Entry(window,width=3)
tim5.grid(column=1, row=6, pady=8)
result1 = Label(window,text="status")
result1.grid(column=2, row=2, pady=8)
result2 = Label(window,text="status")
result2.grid(column=2, row=3, pady=8)
result3 = Label(window,text="status")
result3.grid(column=2, row=4, pady=8)
result4 = Label(window,text="status")
result4.grid(column=2, row=5, pady=8)
result5 = Label(window,text="status")
result5.grid(column=2, row=6, pady=8)
btn = Button(window, text="Add another site", command=clicked)
btn.grid(column=1, row = 0)
Check_Button = Button(
window,
command = SiteCheck,
text='Start checking',
)
Check_Button.grid(row=0, column=2)
window.mainloop()
CodePudding user response:
try this but i am do it without time sleep you can add later
import tkinter as tk
from tkinter import ttk
import requests
import time
from tkinter import *
from tkinter import messagebox
data_list = []
window = Tk()
window.geometry('400x700')
window.title("SiteChecker")
def set_input(obj, value):
obj.delete(1.0, "END")
obj.insert("END", value)
def SiteCheck():
#res=int(tim1.get())
#time.sleep(res)
for data in data_list:
url = data[0].get()
status = data[2]
if not str(url).startswith('http'):
continue
print(url)
Get_Response = None
try:
Get_Response = requests.get(url)
except:
status.config(text = 'status bad')
continue
if Get_Response.status_code == 200:
status.config(text = 'status ok')
pass
#as I understand it, you need to make a "for" loop, but I don't understand how to implement
else:
status.config(text = 'status bad')
def clicked():
txt = Entry(window, width=18)
txt.grid(column=0, pady=8)
txt_row = txt.grid_info()['row']
tim = Entry(window, width=3)
tim.grid(row=txt_row, column=1, pady=8)
txt_row = tim.grid_info()['row']
result1 = Label(window,text="status")
result1.grid(row=txt_row, column=2, pady=8)
data_list.append([txt, tim, result1])
lbl1 = Label(window, text="Enter references:")
lbl1.grid(column=0, row=1)
lbl2 = Label(window, text="Enter the test time: ")
lbl2.grid(column=1, row=1)
lbl3 = Label(window, text="Availability status ")
lbl3.grid(column=2, row=1)
for loop in range(2 ,6):
txt1 = Entry(window,width=18)
txt1.grid(column=0, row=loop, pady=8)
tim1 = Entry(window,width=3)
tim1.grid(column=1, row=loop, pady=8)
result1 = Label(window,text="status")
result1.grid(column=2, row=loop, pady=8)
data_list.append([txt1, tim1, result1])
btn = Button(window, text="Add another site", command=clicked)
btn.grid(column=1, row = 0)
Check_Button = Button(
window,
command = SiteCheck,
text='Start checking',
)
Check_Button.grid(row=0, column=2)
window.mainloop()