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:
Change the import to Tkinter. I think it should run with Python2.7
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!
All humans together. We don't need politicians!