Python Forum
TKINTER - Change font color for night or day
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TKINTER - Change font color for night or day
#2
Maybe some thing along these lines. The time for blue is set between 1 pm and 3:59 pm then changes.
import tkinter as tk
from time import strftime


class Clock:
    def __init__(self, parent=None):
        # following allows importing class into another GUI framework.
        if not parent:
            self.parent = tk.Tk()
        else:
            self.parent = parent

        self.parent.title('My Clock')
        self.parent.geometry('200x50+50+50')

        self.clock = tk.Label(self.parent, borderwidth = 2, height=2, relief=tk.SOLID)
        self.clock.pack(padx=5, pady=2, fill=tk.BOTH)
        self.clock.configure(fg='blue')
        self.clock['text'] = strftime("%I:%M:%S")

        self.time_now = strftime("%I:%M:%S")
        self.increnment_time()

        if not parent:
            self.parent.mainloop()


    def display_time(self):
        self.clock['text'] = strftime("%I:%M:%S")
        hour = strftime('%I')
        min = strftime('%M')
        # If it is between these hours fg color is blue else it is green
        if hour >= '13' or hour <= '15' and min <= '59':
            self.clock.configure(fg='blue')
        else:
            self.clock.configure(fg='green')

    def increnment_time(self):
        self.display_time()
        # call self every 1000 ms (1 sec)
        self.clock.after(1000, self.increnment_time)

if __name__ == '__main__':
    clk = Clock()
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
RE: TKINTER - Change font color for night or day - by menator01 - May-24-2020, 08:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,938 Aug-23-2022, 09:11 PM
Last Post: Extra
  [Tkinter] Tkinter don't change the image DQT 2 1,641 Jul-22-2022, 10:26 AM
Last Post: menator01
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,818 May-23-2022, 03:44 PM
Last Post: bigmac
Question [Tkinter] Change Treeview column color? water 3 9,673 Mar-04-2022, 11:20 AM
Last Post: Larz60+
  Can't get tkinter button to change color based on changes in data dford 4 3,457 Feb-13-2022, 01:57 PM
Last Post: dford
  how to change font size barryjo 4 3,924 Jan-26-2022, 08:46 PM
Last Post: menator01
  [Tkinter] Trouble changing Font within tkinter frame title AnotherSam 1 4,156 Sep-30-2021, 05:57 PM
Last Post: menator01
  tkinter change the text of the checkbox zazas321 1 3,865 Sep-17-2021, 06:19 AM
Last Post: zazas321
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 5,141 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  Tkinter menu font size -method to change tonycat 2 7,882 Oct-11-2020, 02:43 AM
Last Post: tonycat

Forum Jump:

User Panel Messages

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