Python Forum
Needing to Check Every Quarter Hour
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Needing to Check Every Quarter Hour
#7
Glad I could help. Posting changed code I have in case it may help someone else.

import tkinter as tk
import json, requests
from datetime import datetime, timedelta
from time import strftime


class Window:
    def __init__(self, parent):

        # Get location - lat and long
        location = json.loads(requests.get('http://ipinfo.io/json').text)['loc']
        lat, lon = location.split(',')

        # Get sunrise/sunset and format
        sunurl = 'https://api.sunrise-sunset.org/json'
        params = {'lat': lat, 'lon': lon, 'formatted': 0, 'date': 'today'}
        input_format = '%Y-%m-%dT%H:%M:%S+00:00'
        output_format = '%H:%M:%S'
        sunpage = requests.get(sunurl, params).json()

        self.sunrise = sunpage['results']['sunrise']
        self.sunrise = datetime.strptime(self.sunrise, input_format)
        self.sunrise = self.sunrise + timedelta()
        self.sunrise = self.sunrise.strftime(output_format)

        self.sunset = sunpage['results']['sunset']
        self.sunset = datetime.strptime(self.sunset, input_format)
        self.sunset = self.sunset + timedelta()
        self.sunset = self.sunset.strftime(output_format)

        # Helps with grid layout stretching
        parent.columnconfigure(0, weight=1)
        parent.columnconfigure(1, weight=1)

        # Create text label to display header
        time_label = tk.Label(parent, text='Current Local Time: ')
        time_label['relief'] = 'ridge'
        time_label.grid(column=0, row=0, sticky='new')

        # Create text label for displaying clock
        self.time_label = tk.Label(parent)
        self.time_label['relief'] = 'ridge'
        self.time_label.grid(column=1, row=0, sticky='new')

        # Create label that will span two columns for displaying a color background
        self.color_label = tk.Label(parent, width=5)
        self.color_label['relief'] = 'ridge'
        self.color_label.grid(column=0, columnspan=2, row=1, sticky='news')

        # Call the update function
        self.update()

    # Define the update function
    def update(self):

        # Updates the clock display every one second
        self.time_label['text'] = strftime('%H:%M:%S')
        self.time_label.after(1000, self.update)

        # Create a list of the minutes we want to check. This case every 15 minutes
        quarters = [00, 15, 30, 45]

        # Loop through the list and checking it against the current time minute
        # If the current minute is in the minutes list change background and foreground colors
        # of the clock. Colors change back to default after one minute
        minute = int(self.time_label['text'][3:5])
        if minute in quarters:
            self.time_label['fg'] = 'lime'
            self.time_label['bg'] = 'darkgreen'
        else:
            self.time_label['fg'] = 'black'
            self.time_label['bg'] = 'gray96'

        # Check the current time label against the sunrise and sunset time from
        # sunrise-sunset.org If the time is between sunrise and sunset, the background color is yellow.
        # If the time is between sunset and sunrise, the background color is navy.
        if self.time_label['text'] >= self.sunrise and self.time_label['text'] <= self.sunset:
            self.color_label['bg'] = 'yellow'
        else:
            self.color_label['bg'] = 'navy'


def main():
    root = tk.Tk()
    root['padx'] = 10
    root['pady'] = 6
    Window(root)
    root.mainloop()

if __name__ == '__main__':
    main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
Needing to Check Every Quarter Hour - by bill_z - Feb-08-2022, 04:30 PM
RE: Needing to Check Every Quarter Hour - by bill_z - Feb-08-2022, 05:01 PM
RE: Needing to Check Every Quarter Hour - by bill_z - Feb-08-2022, 07:19 PM
RE: Needing to Check Every Quarter Hour - by menator01 - Feb-08-2022, 07:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  needing some help to write some code for a game calculator rymdaksel 1 581 Jan-02-2024, 09:56 AM
Last Post: deanhystad
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,654 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Struggling for the past hour to define function and call it back godlyredwall 2 2,343 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  Noob needing guidance.... bako 0 1,938 Mar-29-2020, 06:55 PM
Last Post: bako
  Global Variables - Some Points Needing Clarification. adt 4 3,095 Nov-30-2019, 01:23 PM
Last Post: adt
  Beginner needing advice with data files JFI2019 2 2,313 Nov-06-2019, 04:56 PM
Last Post: JFI2019
  Typical beginner needing some help foxter00 1 2,764 May-08-2019, 11:46 PM
Last Post: michalmonday
  Different prices per hour kemper 1 2,119 Jan-29-2019, 11:06 AM
Last Post: Larz60+
  New user/beginner needing help oldDog 3 3,354 Apr-17-2018, 02:31 PM
Last Post: oldDog
  Beginner needing help! Franco 2 3,354 Jul-29-2017, 12:56 PM
Last Post: Franco

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020