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
#2
This code can be greatly simplified.
Reply
#3
I guess I am not understanding the response for my request for help.  If I have asked an incorrect question, please advise.  Otherwise, I just need a bit of guidance as to how to change button colors when they are depressed.
Reply
#4
Sorry, I was sidetracked and didn't finish.

you're already setting the color, for example:
button1 = Button(topframe, padx=16, bd=8, text="AIRCARD A-1 ON LINE", bg="green", fg="black", command=led1_on)bg="green"  - sets the background color.
bg="green" - sets backgrund color
fg="black" - sets foreground color.

you can use the standard color names, or rgb values like "#ff0000"

There are other colors that can be set, for example:

activebackground - Background color when the button is under the cursor.
highlightbackground - Color of the focus highlight when the widget does not have focus.
etc.
You can find out about them in the tkinter reference here: http://infohost.nmt.edu/tcc/help/pubs/tk...utton.html

color names can be found here: https://www.w3schools.com/colors/colors_names.asp
schemes (colors that work well together) can be found here: https://www.w3schools.com/colors/colors_schemes.asp

and to cook your own, there is a color picker tool here: https://www.w3schools.com/colors/colors_picker.asp

My original comment is about the repetition in your code.
You can greatly simplify the code by using functions (or a class with methods).
I would guess code reduction on the order of 60 to 80 %
Reply
#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
#6
Is this the correct way to insert my code. Is this what you mean by code tags (BBcode)?


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
#7
(Aug-10-2017, 01:06 PM)Crawford3 Wrote: Is this the correct way to insert my code. Is this what you mean by code tags (BBcode)?

It is the correct usage of 'code' tag, however, you have no indentation. The easiest way to paste code, errors and output between their respective tags is to copy your code then use the key combination "Ctrl + Shift + V" to paste. There is a "Preview Post" button, use it to make sure it is appearing properly before you actually post.  It seems all you did was copy from your previous post and then paste it between the tags, wasting space and time, since no one is going to bother if your not going to make the effort to post properly.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
It is a bit tricky. You can misuse command and give as first argument the button itself as reference to the function. Inside the function you can do something like a toggle function with an if-condition.

Here an example:

from functools import partial
from tkinter import Tk
from tkinter import Button


def toggle(button, to_send):
"""
This function gets the reference to the button itself and the command which
should be send to serial port...
"""
if button['bg'] == 'green':
button['bg'] = 'red'
else:
button['bg'] = 'green'
button['activebackground'] = button['bg']
print('Button pressed')
print('Sending:', to_send)
# your code to send it to the serial port


# Your way
root = Tk()
b1 = Button(root, text='YourButton1', bg='red', activebackground='red')
b1['command'] = partial(toggle, b1, 'This is what you can send to serial port...')
b1.pack()


# doing it programatically
labels = ['Button {}'.format(n) for n in range(1,17)] # Text 1 - 16
text_commands = ['{:x}'.format(n) for n in range(0,16)] # Command 0 - 15 in HEX
buttons = [Button(root, text=label, bg='red', activebackground='red') for label in labels] # prepare the Buttons
# doing the rest and pack finally
for button, text_command in zip(buttons, text_commands):
button['command'] = partial(toggle, button, text_command)
button.pack()

Button(root, text='Quit', command=root.destroy).pack()

root.mainloop()
Remind, it's Python 3.
Change the import to Tkinter. I think it should run with Python2.7
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


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