Python Forum
Python/Tkinter button color changing
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python/Tkinter button color changing
#5
Thanks for the reply. What I have accomplished is that I can change the parameters of the buttons when each is depressed but what I need to do is to have that button change color depending on the data returned to Python from an Arduino anlog input bit of data. In other words, I depress the GUI button with the Pytho/Tkinter GUI screen and my mouse and that sends a command to the Arduino which switches a relay. I would like the button to change colors and / or text only when the Arduino sends back data to Python that the relay has actually changed position. I have a contact from the relay that sends a fixed voltage to an Arduino analog input and I can see that value in the data stam but I just CAN NOT figure out how to use that bit of data to modify the button color. Help please.

This works it just doesn't really reflect the relays' switching status. The label between the buttons changes color and text when each button is depressed. I need those labels to do their thing only when the Arduino data says they have changed state.



import serial
import Tkinter

arduinoData = serial.Serial('com10', 9600)





def changeCol0s():
butt0["bg"]="red"
butt0["fg"]="black"
butt0["text"]="AIRCARD#30\nSIM STATUS\nutility SIM"
return

def changeCol0u():
butt0["bg"]="green"
butt0["fg"]="black"
butt0["text"]="AIRCARD#30\nSIM STATUS\nnormal SIM"

def changeCol1s():
butt1["bg"]="red"
butt1["fg"]="black"
butt1["text"]="AIRCARD#31\nSIM STATUS\nutility SIM"
return

def changeCol1u():
butt1["bg"]="green"
butt1["fg"]="black"
butt1["text"]="AIRCARD#31\nSIM STATUS\nnormal SIM"

def changeCol2s():
butt2["bg"]="red"
butt2["fg"]="black"
butt2["text"]="AIRCARD#32\nSIM STATUS\nutility SIM"
return

def changeCol2u():
butt2["bg"]="green"
butt2["fg"]="black"
butt2["text"]="AIRCARD#32\nSIM STATUS\nnormal SIM"

def changeCol3s():
butt3["bg"]="red"
butt3["fg"]="black"
butt3["text"]="AIRCARD#33\nSIM STATUS\nutility SIM"
return

def changeCol3u():
butt3["bg"]="green"
butt3["fg"]="black"
butt3["text"]="AIRCARD#33\nSIM STATUS\nnormal SIM"

def changeCol4s():
butt4["bg"]="red"
butt4["fg"]="black"
butt4["text"]="AIRCARD#34\nSIM STATUS\nutility SIM"
return

def changeCol4u():
butt4["bg"]="green"
butt4["fg"]="black"
butt4["text"]="AIRCARD#34\nSIM STATUS\nnormal SIM"

def changeCol5s():
butt5["bg"]="red"
butt5["fg"]="black"
butt5["text"]="AIRCARD#35\nSIM STATUS\nutility SIM"
return

def changeCol5u():
butt5["bg"]="green"
butt5["fg"]="black"
butt5["text"]="AIRCARD#35\nSIM STATUS\nnormal SIM"

def changeCol6s():
butt6["bg"]="red"
butt6["fg"]="black"
butt6["text"]="AIRCARD#36\nSIM STATUS\nutility SIM"
return

def changeCol6u():
butt6["bg"]="green"
butt6["fg"]="black"
butt6["text"]="AIRCARD#36\nSIM STATUS\nnormal SIM"



def led1_on():
arduinoData.write('2')
print "Aircard 30 is now using the Utility SIM"


def led1_off():
arduinoData.write('3')
print "Aircard 30 is now using the Normal SIM"

def led2_on():
arduinoData.write('4')
print "Aircard 31 is now using the Utility SIM"

def led2_off():
arduinoData.write('5')
print "Aircard 31 is now using the Normal SIM"

def led3_on():
arduinoData.write('6')
print "Aircard 32 is now using the Utility SIM"

def led3_off():
arduinoData.write('7')
print "Aircard 32 is now using the Normal SIM"

def led4_on():
arduinoData.write('8')
print "Aircard 33 is now using the Utility SIM"

def led4_off():
arduinoData.write('9')
print "Aircard 33 is now using the Normal SIM"

def led5_on():
arduinoData.write('A')
print "Aircard 34 is now using the Utility SIM"

def led5_off():
arduinoData.write('B')
print "Aircard 34 is now using the Normal SIM"

def led6_on():
arduinoData.write('C')
print "Aircard 35 is now using the Utility SIM"

def led6_off():
arduinoData.write('D')
print "Aircard 35 is now using the Normal SIM"

def led7_on():
arduinoData.write('E')
print "Aircard 36 is now using the Utility SIM"

def led7_off():
arduinoData.write('F')
print "Aircard 36 is now using the Normal SIM"




import sys
from Tkinter import*
from time import sleep


root = Tk()
frame = Frame(root)
frame.pack()

root.title("SIM SWAP SYSTEM")

num1=StringVar()

topframe = Frame(root)
topframe.pack(side = TOP)
txtDisplay = Button(frame, bg="green", fg="black", text="Medical Justice Services, Inc. / Dental Justice / eMerit\nSIM SWAP SYSTEM\nAIRCARDS 30-36")
txtDisplay.pack(side = TOP)


frame0 = Frame(root)
frame0.pack(side=TOP)

button0s = Button(frame0, padx=16, bd=8, text="AIRCARD#30\npress this button\nto swap to utility SIM", command = lambda: [changeCol0s(),led1_on()])
button0s.pack(side=LEFT)
butt0 = Label(frame0, text="AIRCARD#30\nSIM STATUS\n normal SIM.", bg="green", font="Arial 8 bold", width=15, height=3, anchor="n")
butt0.pack(side=LEFT)
button0u = Button(frame0, padx=16, bd=8, text="AIRCARD#30\npress this button\nto swap to normal SIM", command = lambda:[changeCol0u(),led1_off()])
button0u.pack(side=LEFT)


frame1 = Frame(root)
frame1.pack(side = TOP)

button1s = Button(frame1, padx=16, bd=8, text="AIRCARD#31\npress this button\nto swap to utility SIM", command = lambda: [changeCol1s(),led2_on()])
button1s.pack(side = LEFT)
butt1 = Label(frame1, text="AIRCARD#31\nSIM STATUS\n normal SIM.", bg="green", font="Arial 8 bold", width=15, height=3, anchor="n")
butt1.pack(side=LEFT)
button1u = Button(frame1, padx=16, bd=8, text="AIRCARD#31\npress this button\nto swap to normal SIM", command = lambda:[changeCol1u(),led2_off()])
button1u.pack(side = LEFT)


frame2 = Frame(root)
frame2.pack(side = TOP)

button2s = Button(frame2, padx=16, bd=8, text="AIRCARD#32\npress this button\nto swap to utility SIM", command = lambda: [changeCol2s(),led3_on()])
button2s.pack(side = LEFT)
butt2 = Label(frame2, text="AIRCARD#32\nSIM STATUS\n normal SIM.", bg="green", font="Arial 8 bold", width=15, height=3, anchor="n")
butt2.pack(side=LEFT)
button2u = Button(frame2, padx=16, bd=8, text="AIRCARD#32\npress this button\nto swap to normal SIM", command = lambda:[changeCol2u(),led3_off()])
button2u.pack(side = LEFT)


frame3 = Frame(root)
frame3.pack(side = TOP)

button3s = Button(frame3, padx=16, bd=8, text="AIRCARD#33\npress this button\nto swap to utility SIM", command = lambda: [changeCol3s(),led4_on()])
button3s.pack(side = LEFT)
butt3 = Label(frame3, text="AIRCARD#33\nSIM STATUS\n normal SIM.", bg="green", font="Arial 8 bold", width=15, height=3, anchor="n")
butt3.pack(side=LEFT)
button3u = Button(frame3, padx=16, bd=8, text="AIRCARD#33\npress this button\nto swap to normal SIM", command = lambda:[changeCol3u(),led4_off()])
button3u.pack(side = LEFT)

frame4 = Frame(root)
frame4.pack(side = TOP)

button4s = Button(frame4, padx=16, bd=8, text="AIRCARD#34\npress this button\nto swap to utility SIM", command = lambda: [changeCol4s(),led5_on()])
button4s.pack(side = LEFT)
butt4 = Label(frame4, text="AIRCARD#34\nSIM STATUS\n normal SIM.", bg="green", font="Arial 8 bold", width=15, height=3, anchor="n")
butt4.pack(side=LEFT)
button4u = Button(frame4, padx=16, bd=8, text="AIRCARD#34\npress this button\nto swap to normal SIM", command = lambda:[changeCol4u(),led5_off()])
button4u.pack(side = LEFT)


frame5 = Frame(root)
frame5.pack(side = TOP)

button5s = Button(frame5, padx=16, bd=8, text="AIRCARD#35\npress this button\nto swap to utility SIM", command = lambda: [changeCol5s(),led6_on()])
button5s.pack(side = LEFT)
butt5 = Label(frame5, text="AIRCARD#35\nSIM STATUS\n normal SIM.", bg="green", font="Arial 8 bold", width=15, height=3, anchor="n")
butt5.pack(side=LEFT)
button5u = Button(frame5, padx=16, bd=8, text="AIRCARD#35\npress this button\nto swap to normal SIM", command = lambda:[changeCol5u(),led6_off()])
button5u.pack(side = LEFT)

frame6 = Frame(root)
frame6.pack(side = TOP)

button6s = Button(frame6, padx=16, bd=8, text="AIRCARD#36\npress this button\nto swap to utility SIM", command = lambda: [changeCol6s(),led7_on()])
button6s.pack(side = LEFT)
butt6 = Label(frame6, text="AIRCARD#36\nSIM STATUS\n normal SIM.", bg="green", font="Arial 8 bold", width=15, height=3, anchor="n")
butt6.pack(side=LEFT)
button6u = Button(frame6, padx=16, bd=8, text="AIRCARD#36\npress this button\nto swap to normal SIM", command = lambda:[changeCol6u(),led7_off()])
button6u.pack(side = LEFT)


root.mainloop()
Reply


Messages In This Thread
Python/Tkinter button color changing - by Crawford3 - Jul-24-2017, 06:39 PM
RE: Python/Tkinter button color changing - by Crawford3 - Aug-09-2017, 01:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Virtual Env changing mysql connection string in python Fredesetes 0 415 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 1,142 Oct-25-2023, 09:09 AM
Last Post: codelab
  how to open a popup window in tkinter with entry,label and button lunacy90 1 964 Sep-01-2023, 12:07 AM
Last Post: lunacy90
  Tkinter button images not showing up lunacy90 7 1,690 Aug-31-2023, 06:39 PM
Last Post: deanhystad
Bug tkinter.TclError: bad window path name "!button" V1ber 2 858 Aug-14-2023, 02:46 PM
Last Post: V1ber
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,700 Jul-05-2022, 11:29 PM
Last Post: deanhystad
Photo Visual studio code unable to color syntax on python interpreter tomtom 4 7,074 Mar-02-2022, 01:23 AM
Last Post: tomtom
  Closing Threads and the chrome window it spawned from Tkinter close button law 0 1,758 Jan-08-2022, 12:13 PM
Last Post: law
  tkinter auto press button kucingkembar 2 3,267 Dec-24-2021, 01:23 PM
Last Post: kucingkembar
  changing Python files to .exe alok 2 2,280 Jul-20-2021, 02:49 PM
Last Post: alok

Forum Jump:

User Panel Messages

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