[Tkinter] So what do I need to do change the color this label? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] So what do I need to do change the color this label? (/thread-17165.html) Pages:
1
2
|
So what do I need to do change the color this label? - jpezz - Mar-31-2019 I've adapted this countdown clock program from another member and it works except I want to make the text color change when the countdown hits 30 & 15 seconds. But it won't change the color. I have tried numerous ways, the last two of which (I commented out since neither works) in "colorupdate" (see lines 46 &47). I've searched the web for hours and still don't get it. What am I (a python neophyte) doing wrong? # Python3 import tkinter as tk import sys import time # 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] def count_down(): global Number_Color for t in range(Timer_length, -1, -1): if (t is 30): colourUpdate('yellow') print ('Updated color') fg=Number_Color.get() print (t,fg) elif (t is 15): colourUpdate('red') # format as 2 digit integers, fills with zero to the left # divmod() gives minutes, seconds sf = "{:02d}:{:02d}".format(*divmod(t, 60)) #print(sf) # test time_str.set(sf) root.update() # delay one second time.sleep(1) # create root/main window root = tk.Tk() Number_Color=tk.StringVar() Number_Color.set('blue') print (Number_Color.get()) def colourUpdate(color): Number_Color.set(color) fgx=Number_Color.get() print ('color: ',fgx) #root.configure(fg=Number_Color.get()) #root.update() 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) 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) count_down() # start the GUI event loop root.mainloop() RE: So what do I need to do change the color this label? - Larz60+ - Mar-31-2019 see: https://python-forum.io/Thread-Tkinter-color-a-string-of-characters-in-tkinter?pid=75328#pid75328 RE: So what do I need to do change the color this label? - jpezz - Mar-31-2019 (Mar-31-2019, 08:04 PM)Larz60+ Wrote: see: https://python-forum.io/Thread-Tkinter-color-a-string-of-characters-in-tkinter?pid=75328#pid75328That looks like a way to set multiple colors in text. I want to change the text so that all the numbers which were displayed in blue are now displayed in yellow or red as appropriate as the countdown continues. Will that code work for me? RE: So what do I need to do change the color this label? - Larz60+ - Mar-31-2019 Actually no it won't, I was a bit hasty. It's somewhat different for a Label, though basically the same. you can see the list of attributes here: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/label.html you can highlight, or set foreground and background colors which I think is what you should do. You don't have to change both, but the effect is bolder if you, for example, have a black background with a yellow foreground. RE: So what do I need to do change the color this label? - jpezz - Apr-01-2019 (Mar-31-2019, 11:27 PM)Larz60+ Wrote: Actually no it won't, I was a bit hasty. It's somewhat different for a Label, though basically the same.You still have me confused. I have created the label. I want to change the colors. Are you saying I do label.tag_config(tag_name, fg='yellow',bg='black')to change it after the fact? or something like this? mylabelname.config(fg='yellow',bg='black' ) RE: So what do I need to do change the color this label? - Larz60+ - Apr-01-2019 You can do it either after the fact, or include it in the instantiation of the Label. If done afterwards, it's easy to create a method for changing color on the fly. RE: So what do I need to do change the color this label? - jpezz - Apr-02-2019 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): RE: So what do I need to do change the color this label? - wuf - Apr-02-2019 Hi jpezz Make following modification: label=tk.Label(root, textvariable=time_str, font=label_font, bg='white', fg=Number_Color.get(), relief='raised', bd=3) label.pack(fill='x', padx=5, pady=5)wuf :-) RE: So what do I need to do change the color this label? - jpezz - Apr-02-2019 (Apr-02-2019, 09:15 PM)wuf Wrote: Hi jpezzBut where? Instead of lines 96 & 97 in the main part or 85 and 85 in the function colourUpdate? The latter did not work. RE: So what do I need to do change the color this label? - wuf - Apr-03-2019 Hi jpezz On my side this alteration is working perfectly. Here i place the modified script. The count down starts at 40 in green. With 30 the color of the figures in the label change to yellow (Yellow on a white background is not the best contrast). Reaching 15 the color does change to red. To avoid the import of all parameter i have placed a test line in the script! # 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] # Test line to bypass the argv parameter import! Timer_length, Xsize, Ysize, Xoffset, Yoffset = [40, 300, 300, 100, 100] # # 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) label.pack(fill='x', padx=5, pady=5) # # The line below is a test to see if the color changes # label.config(fg='green') count_down() # start the GUI event loop root.mainloop()wuf :-) |