Python Forum
Python/Tkinter button color changing
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python/Tkinter button color changing
#1
I need a bit of help here.  This program(below) works fine but I would like it if when a button was "depressed" its background color would change.  The left buttons latch a dual coil relay and the right button unlatches it.  Anyone know how to add to my program to get the left button to turn green when it is "depressed" and at the same time get the right button to change to red?  Then when the right button (unlatch) is "depressed" the colors would swap so the right button turn green and the left button would go back to red.

Here is my program:

import serial
import Tkinter

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


   
           
def led1_on():
   arduinoData.write('2') #write(b'1')
   
def led1_off():
   arduinoData.write('3')

def led2_on():
   arduinoData.write('4') #write(b'2')

def led2_off():
   arduinoData.write('5')

def led3_on():
   arduinoData.write('6') #write(b'3')

def led3_off():
   arduinoData.write('7')

def led4_on():
   arduinoData.write('8') #write(b'4')

def led4_off():
   arduinoData.write('9')

def led5_on():
   arduinoData.write('A') #write(b'5')

def led5_off():
   arduinoData.write('B')

def led6_on():
   arduinoData.write('C') #write(b'6')

def led6_off():
   arduinoData.write('D')

def led7_on():
   arduinoData.write('E') #write(b'7')

def led7_off():
   arduinoData.write('F')


   
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="SIM SWAP BUTTON SYSTEM")
txtDisplay.pack(side = TOP)

button1 = Button(topframe, padx=16, bd=8, text="AIRCARD A-1 ON LINE", bg="green", fg="black", command=led1_on)
button1.pack(side = LEFT)
button2 = Button(topframe, padx=16, bd=8, text="AIRCARD A-1 UTILITY", fg="red", command=led1_off)
button2.pack(side = LEFT)


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

button1 = Button(frame1, padx=16, bd=8, text="AIRCARD A-2 ON LINE", fg="black", command=led2_on)
button1.pack(side = LEFT)
button2 = Button(frame1, padx=16, bd=8, text="AIRCARD A-2 UTILITY", fg="red", command=led2_off)
button2.pack(side = LEFT)


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

button1 = Button(frame2, padx=16, bd=8, text="AIRCARD A-3 ON LINE", fg="black", command=led3_on)
button1.pack(side = LEFT)
button2 = Button(frame2, padx=16, bd=8, text="AIRCARD A-3 UTILITY", fg="red", command=led3_off)
button2.pack(side = LEFT)


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

button1 = Button(frame3, padx=16, bd=8, text="AIRCARD A-4 ON LINE", fg="black", command=led4_on)
button1.pack(side = LEFT)
button2 = Button(frame3, padx=16, bd=8, text="AIRCARD A-4 UTILITY", fg="red", command=led4_off)
button2.pack(side = LEFT)

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

button1 = Button(frame4, padx=16, bd=8, text="AIRCARD A-5 ON LINE", fg="black", command=led5_on)
button1.pack(side = LEFT)
button2 = Button(frame4, padx=16, bd=8, text="AIRCARD A-5 UTILITY", fg="red", command=led5_off)
button2.pack(side = LEFT)


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

button1 = Button(frame5, padx=16, bd=8, text="AIRCARD A-6 ON LINE", fg="black", command=led6_on)
button1.pack(side = LEFT)
button2 = Button(frame5, padx=16, bd=8, text="AIRCARD A-6 UTILITY", fg="red", command=led6_off)
button2.pack(side = LEFT)

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

button1 = Button(frame6, padx=16, bd=8, text="AIRCARD A-7 ON LINE", fg="black", command=led7_on)
button1.pack(side = LEFT)
button2 = Button(frame6, padx=16, bd=8, text="AIRCARD A-7 UTILITY", fg="red", command=led7_off)
button2.pack(side = LEFT)


root.mainloop()
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Virtual Env changing mysql connection string in python Fredesetes 0 951 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 3,232 Oct-25-2023, 09:09 AM
Last Post: codelab
  how to open a popup window in tkinter with entry,label and button lunacy90 1 3,141 Sep-01-2023, 12:07 AM
Last Post: lunacy90
  Tkinter button images not showing up lunacy90 7 3,837 Aug-31-2023, 06:39 PM
Last Post: deanhystad
Bug tkinter.TclError: bad window path name "!button" V1ber 2 2,209 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 2,752 Jul-05-2022, 11:29 PM
Last Post: deanhystad
Photo Visual studio code unable to color syntax on python interpreter tomtom 4 11,793 Mar-02-2022, 01:23 AM
Last Post: tomtom
  Closing Threads and the chrome window it spawned from Tkinter close button law 0 2,332 Jan-08-2022, 12:13 PM
Last Post: law
  tkinter auto press button kucingkembar 2 4,802 Dec-24-2021, 01:23 PM
Last Post: kucingkembar
  changing Python files to .exe alok 2 2,856 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