Python Forum
Can't change the colour of Tk button text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't change the colour of Tk button text
#1
Please help! This code doesn't work, but doesn't produce an error.

from tkinter import *
master = Tk()
button = Button(master, text="some text", bg="red")
button.grid(column=0, row=0)

This code is a simplified version of some code I'm using in a much bigger project. I am using Python 3 on a Mac and Raspberry Pi. The colour change option doesn't seem to work on the Mac or the Pi for buttons, but does for labels, etc., .
Reply
#2
with ttk you can use a style command, can't remember if this works with plain tkinter, you'll have to check.
But, with plain tkinter you can change background and foreground colors to get same effect, example:
>>> import tkinter
>>> root = tkinter.Tk()
>>> xx = tkinter.Button(root, text='Hello', bg="white", fg="blue")
>>> xx.pack()
>>> root.mainloop()
Reply
#3
Thank you for your response.
I've tried your example code and that doesn't seem to work either.
Reply
#4
I tried it, and indeed it does work.
if you need it in a file to try, create a file named trycolor.py
and place this code in the file:

trycolor.py:
import tkinter

root = tkinter.Tk()
xx = tkinter.Button(root, text='Hello', bg="white", fg="blue")
xx.pack()
root.mainloop()
Then run it (from command line) using:
python trycolor.py
Reply
#5
ah, a bit more experimenting.. it does work on my Pi, but not on my Mac..
Reply
#6
(Feb-27-2019, 09:02 PM)Pilover Wrote: ah, a bit more experimenting.. it does work on my Pi, but not on my Mac..

This failure of Tkinter Buttons to have their color changed on Mac is a well-known and long-standing issue. It's only for Buttons, not Labels.

I wrote a project called TTWidgets which supports multi-font, multi-color, multi-image Buttons and Labels. It's based on the Label widget, so it works around the MacOS Button color issue.

Check out https://pypi.org/project/ttwidgets/

from ttwidgets import TTButton

The TTButton API is a superset of the Tkinter.Button API.
Larz60+ write Nov-16-2022, 03:39 AM:
This is necroposting. Please read: https://python-forum.io/misc.php?action=help&hid=51
Reply
#7
There is also CustomTkinter, which is close to the tkinter widgets. Note that bg in tkinter is fg in customtkinter, and fg is text_color.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 856 Aug-23-2022, 09:11 PM
Last Post: Extra
  [WxPython] [SOLVED] How to change button label? Winfried 3 741 May-31-2022, 06:37 PM
Last Post: Winfried
  Can't get tkinter button to change color based on changes in data dford 4 2,206 Feb-13-2022, 01:57 PM
Last Post: dford
  tkinter change the text of the checkbox zazas321 1 2,285 Sep-17-2021, 06:19 AM
Last Post: zazas321
  [Tkinter] Make my button text update? Skata100 1 1,446 Aug-07-2021, 05:37 AM
Last Post: deanhystad
  Updating button text based upon different variable values knoxvilles_joker 0 1,673 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  How to dynamically change radiobutton text kenwatts275 2 2,184 Mar-05-2021, 02:25 AM
Last Post: deanhystad
  Making a colour field Leo_Red 11 3,331 Jan-22-2021, 04:15 PM
Last Post: BashBedlam
  tkinter | Button color text on Click Maryan 2 2,557 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Trying to change font size w/o changing button size python63 3 7,873 Aug-05-2020, 01:04 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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