Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

help with button to change text colour

Sat Apr 04, 2015 5:17 pm

Hi all, im new to Python and the raspberry. im trying to make a fairly complex tester, im starting with the gui and ive been able to get a functioning button and separate text window to display messages but now I want to put a colour to the background of that window with each of the 2 messages I currently have it displaying but anything I try from searching online fails to work with my current code.. # ive hashed what colours im trying to have appear as back ground

import Tkinter as tk

from Tkinter import *
import time


class GridDemo(Frame):
def var1(self):
self.label4String.set("Panel Test Began") # I Want This Background To Be Green
if self.button1["text"] == "Begin Panel Test":
print "You have started the panel test:", self.label4String.get()
self.button1["text"] = "Stop Panel Test"
else:
self.button1["text"] = "Begin Panel Test"
self.label4String.set("Panel Test Ended") # I Want This Background To Be Red
print "You have stopped the panel test:", self.label4String.get()
time.sleep(0.5)

def __init__(self):
Frame.__init__(self)
self.master.title( "Grid Demo" )
self.variable = "Start Variable"

self.master.rowconfigure( 0, weight=1)
self.master.columnconfigure( 0, weight=1)
self.grid(sticky=W+E+N+S)

self.button1 = Button(self, text = "Begin Panel Test", command = self.var1)
self.button1.grid(row=1, column=1, sticky=W+E+N+S)

self.button2 = Button(self, text="Quit", command=exit)
self.button2.grid(row=1, column=2, sticky=W+E+N+S)

self.label4String = StringVar()
self.label4 = Label(self, textvariable=self.label4String)
self.label4.grid(row=2, column=1, columnspan=2, sticky=W+E+N+S)

self.rowconfigure(1, weight=1)
self.columnconfigure(1, weight=1)

def main():

GridDemo().mainloop()

if __name__ == '__main__':
main()

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: help with button to change text colour

Sat Apr 04, 2015 5:47 pm

Try this, which will set the background colour to dark red then change it to green

Code: Select all

self.label4String.config(bg = 'dark red')
time.sleep(0.5)
self.label4String.config(bg = 'green')
time.sleep(0.5)
You can change foreground colour with

Code: Select all

self.label4String.config(fg='orange')
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: help with button to change text colour

Sat Apr 04, 2015 6:02 pm

thank you for you reply, I receive this error " self.label4String.config(bg = 'green') AttributeError: StringVar instance has no attribute 'config'" when ive pasted into my code

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: help with button to change text colour

Sat Apr 04, 2015 6:15 pm

It works with this

Code: Select all

defaultColour = rootWindow.cget("bg")
clock = Label(rootWindow, font = ('fixed', 20), height = 2)
clock.config(bg = 'dark red')
time.sleep(0.5)
clock.config(bg = 'green')
time.sleep(0.5)
clock.config(bg=defaultColour)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: help with button to change text colour

Sat Apr 04, 2015 6:34 pm

I was able to do it by re writing my code like this
import Tkinter as tk

import time

def toggle_text():
if button["text"] == "Begin Panel Test":
button["text"] = "Stop Panel Test"
label["text"] = "Panel Test Began"
label.config(bg = 'green')
time.sleep(0.5)
else:
button["text"] = "Begin Panel Test"
label["text"] = "Panel Test Ended"
label.config(bg = 'dark red')
time.sleep(0.5)


root = tk.Tk()
root.title("Click the Button")

button = tk.Button(text="Begin Panel Test", width=12, command=toggle_text)
button.pack(padx=100, pady=10)
label = tk.Label(text=" ", width=24, font='bold')
label.pack(padx=200, pady=10)

root.mainloop()

Thank you for your help, I used your code on this

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: help with button to change text colour

Sat Apr 04, 2015 6:36 pm

You need to configure the Label that holds the text, not the text string itself.

Code: Select all

self.label4.config(bg='green'
Or use the dictionary method

Code: Select all

self.label4['bg']='green'
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”