Feb-08-2022, 07:51 PM
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
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags