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
#1
Hi Everyone,

I would like to know if it's possible, with TKinter, to change font color from 10am to 10pm and form 10pm to 10 am, for example, from 10am to 10pm, it's day, font is green .. from 10pm to 10am it's night, font is blue.. I know how to change color but this is with the time management that i don't see how to do..

Here is my code
from tkinter import *
from time import *


class DigitalClock:

    def __init__(self, window):
        self.time_init = ""
        self.label = Label(window, font = ("arial", "180", "bold"),
                           bg = "black", fg = "lime")
        self.label.pack(fill ="both", expand = 1)
        self.label.after(1000, self.timer)

    # ------------------------------------------------------------------------
    def timer(self):
        self.time2 = strftime("%H : %M\n%S")

        if self.time2 != self.time_init:
            self.time_init = self.time2
            self.label.config(text = self.time2)
            self.label.after(1000, self.timer)

# ============================================================================
if __name__ == "__main__":
    master = Tk()
    master.geometry("1024x600")
    master.title("Digital Clock")
    clock = DigitalClock(master)
    # -- Fullscreen Mode
    #master.attributes('-fullscreen', True)
    # -- Deactivate mouse cursor
    #master.config(cursor = "none")
    master.mainloop()
Thank you all :)
Reply
#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()
Reply
#3
Many many thanks, that's what i wanted !! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 480 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  Tkinter menu font size -method to change tonycat 2 925 Oct-11-2020, 02:43 AM
Last Post: tonycat
  tkinter | Button color text on Click Maryan 2 449 Oct-09-2020, 08:56 PM
Last Post: Maryan
  Tkinter - How can I change the default Notebook border color? TurboC 4 1,540 Oct-06-2020, 10:35 PM
Last Post: TurboC
  [Tkinter] How to Print a list = ['a','b','c'], using tkinter along with a custom font? Pleiades 2 371 Sep-15-2020, 03:54 PM
Last Post: Pleiades
  [Tkinter] Trying to change font size w/o changing button size python63 3 2,841 Aug-05-2020, 01:04 AM
Last Post: Larz60+
  [tkinter] color change for hovering over button teacher 4 1,858 Jul-04-2020, 06:33 AM
Last Post: teacher
  [PyQt] Increase text size and change color based on temp pav1983 5 579 Jun-22-2020, 10:52 PM
Last Post: menator01
  Tkinter help (color) Florent 2 668 Mar-01-2020, 02:59 PM
Last Post: Florent
  [PyGUI] Python 3.8.1 Tkinter Widget stete change sasiap 4 653 Feb-14-2020, 10:38 AM
Last Post: sasiap

Forum Jump:

User Panel Messages

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