Python Forum
Python/Tkinter button color changing
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python/Tkinter button color changing
#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


Messages In This Thread
Python/Tkinter button color changing - by Crawford3 - Jul-24-2017, 06:39 PM
RE: Python/Tkinter button color changing - by DeaD_EyE - Aug-10-2017, 02:07 PM

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