Python Forum
[Tkinter] So what do I need to do change the color this label?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] So what do I need to do change the color this label?
#7
This is my code.
#  tk_counter_down101.py
# count down seconds from a given minute value
# using the Tkinter GUI toolkit that comes with Python
# tested with Python27 and Python33
#
#
# Modified by J. Pezzano - 4/02/19
#
# run command: python3 Countdown_Timer.py [X Size] [Y Size] [X offset] [Y offset] [Timer Start]
#
# Python3

import tkinter as tk
#import ttk
import sys
import os  
import time
import signal 
 

# Get variables
#
Timer_length=sys.argv[1]
Timer_length=int(Timer_length)
Xsize=sys.argv[2]
Ysize=sys.argv[3]
Xoffset=sys.argv[4]
Yoffset=sys.argv[5]
filename=sys.argv[6]


#
# Handle a Signal if we get a 'kill 2' or a 'kill, the latter is a 'kill 15'
#
def receiveSignal(signalNumber, frame):
    global CurrentCount  
    print('Received:', signalNumber)
    file = open(filename, "w")
    file.write(str(CurrentCount))
    file.close()
    exit(0)
    return

def count_down():
    global Number_Color
    global CurrentCount
    global root
    for CurrentCount in range(Timer_length, -1, -1):
        if (CurrentCount is 30): 
            colourUpdate('yellow')
            print ('Updated color')
            fg=Number_Color.get()
            print (CurrentCount,fg)
        elif (CurrentCount is 15): colourUpdate('red')
        # format as 2 digit integers, fills with zero to the left
        # divmod() gives minutes, seconds
        sf = "{:02d}:{:02d}".format(*divmod(CurrentCount, 60))
        #print(sf)  # test
        time_str.set(sf)
        root.update()
        # delay one second
        time.sleep(1)

#
# Set that we want to handle some signals
#
signal.signal(signal.SIGINT, receiveSignal)
signal.signal(signal.SIGTERM, receiveSignal)

#
# create root/main window
root = tk.Tk()

Number_Color=tk.StringVar()
Number_Color.set('blue')
print (Number_Color.get())

def colourUpdate(color):
    global root
    global label
    Number_Color.set(color)
    fgx=Number_Color.get()
    print ('color: ',fgx)
    label.config(fg = Number_Color.get())
    root.update()
    return


root.overrideredirect(1)
# Set geometry size and location of Picture
root.geometry('%sx%s+%s+%s' % (Xsize, Ysize, Xoffset, Yoffset))
time_str = tk.StringVar()
# create the time display label, give it a large font
# label auto-adjusts to the font
label_font = ('helvetica', 40)
label=tk.Label(root, textvariable=time_str, font=label_font, bg='white', 
         fg=Number_Color.get(), relief='raised', bd=3).pack(fill='x', padx=5, pady=5)
#
# The line below is a test to see if the color changes
#
label.config(fg = 'yellow')
count_down()
# start the GUI event loop
root.mainloop()
After creating the Label, it calls function count_down which loops. If the count reaches 30 or 15, then color_update is called. The config fails so I tried immediately calling it after creating the widget
label=tk.Label(root, textvariable=time_str, font=label_font, bg='white', 
         fg=Number_Color.get(), relief='raised', bd=3).pack(fill='x', padx=5, pady=5)
#
# The line below is a test to see if the color changes
#
label.config(fg = 'yellow')
and it also fails with this error. What's wrong?
Quote:Traceback (most recent call last):
File "Countdown_Timer.py", line 101, in <module>
label.config(fg = 'yellow')
AttributeError: 'NoneType' object has no attribute 'config'
Reply


Messages In This Thread
RE: So what do I need to do change the color this label? - by jpezz - Apr-02-2019, 08:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 5,017 Aug-23-2022, 09:11 PM
Last Post: Extra
  [WxPython] [SOLVED] How to change button label? Winfried 3 2,150 May-31-2022, 06:37 PM
Last Post: Winfried
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,915 May-23-2022, 03:44 PM
Last Post: bigmac
Question [Tkinter] Change Treeview column color? water 3 9,781 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,500 Feb-13-2022, 01:57 PM
Last Post: dford
  [tkinter] color change for hovering over button teacher 4 8,679 Jul-04-2020, 06:33 AM
Last Post: teacher
  [PyQt] Increase text size and change color based on temp pav1983 5 3,251 Jun-22-2020, 10:52 PM
Last Post: menator01
  [Tkinter] Change Label Every 5 Seconds gw1500se 4 6,987 May-26-2020, 05:32 PM
Last Post: gw1500se
  TKINTER - Change font color for night or day Ayckinn 2 3,934 May-24-2020, 09:25 PM
Last Post: Ayckinn
  [Tkinter] Python 3 change label text gw1500se 6 4,807 May-08-2020, 05:47 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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