0

There is code that checks the availability of sites.

How can we make the status for each site to change at a user defined time (i.e. the time for each site can be different) The problem is that the number of sites is not limited, because you can add more lines to the application, so I do not understand how to implement it.

I attach a picture of how it looks like:

1

Code:

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():
    

    # time.sleep
    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
             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()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • You do not use sleep for that - sleep lets the GUI freeze. You need to use events that proc every n .. times - multiples of something like this: [How can I schedule updates (f/e, to update a clock) in tkinter?](https://stackoverflow.com/questions/2400262/how-can-i-schedule-updates-f-e-to-update-a-clock-in-tkinter) – Patrick Artner Feb 02 '23 at 13:53
  • Do you mean that the *user defined time* is the maximum time to wait for the response from the URL? – acw1668 Feb 02 '23 at 15:23

1 Answers1

0

See here (How do I make my function run every second) how to use tkinter after() to achieve what you want. time.sleep won't work for your purpose because it is blocking the execution of the entire code.

From the link given in the comment to your question by Patrick Artner :

Tkinter root windows have a method called after() which can be used to schedule a function to be called after a given period of time. If that function itself calls after() you've set up an automatically recurring event.

Claudio
  • 7,474
  • 3
  • 18
  • 48