Python 2.7 Tkinter how to change text color of a button's text Python 2.7 Tkinter how to change text color of a button's text tkinter tkinter

Python 2.7 Tkinter how to change text color of a button's text


so taking a look right here you can see that the "foreground" and "fg" option is the same. But this is only the case in the new version of tkinter for python3, if you are using an older version for python2.7 you have to use the "fg" option.

btn = Button(root, fg='red') #Creates a button with red text

If you would like to change the text color afterwards you can achieve this by using the config function:

btn.config(fg='blue') #Changes the text color to blue

I hope this clears things up a bit.keep coding ;D


Because you are doing global imports (rarely ever a good idea), and because you import ttk after tkinter. Both libraries define a Button widget, so the ttk Button is overriding the tkinter Button. The ttk Button has no foregroundoption.

You should stop using global imports to eliminate this problem:

import Tkinter as tkimport ttk...root = tk.Tk()...tk.Button(...)


I use fg

button1 = tk.Button(root, text='hello', fg='red')

edit: hm, actually, both fg and foreground work for me. If you don't bother with the color, does everything else work? It may be that some other error is propagating down. Here is an example of a simple Hello World program using tkinter. See if it works for you. I think the capitalization of tkinter has changed between Python 2 and 3. This is for Python 3.

import tkinter as tkclass Application(tk.Frame):    def __init__(self, master=None):        super().__init__(master)        self.master = master        self.grid()        self.create_widgets()    def create_widgets(self):        self.TVB1 = tk.StringVar(self, value='hello, there')        B1 = tk.Button(self)        # this is an example of how you can define parameters after        # defining the button        B1["textvariable"] = self.TVB1        B1["command"] = self.say_hi        B1.grid(row=0,column=0)        self.TVE1 = tk.StringVar(self, value='wubwub')        E1 = tk.Entry(self, textvariable=self.TVE1)        E1.grid(row=1, column=0)        # and this is how you can define parameters while defining        # the button        Quit = tk.Button(self, text='QUIT', fg='red',                              command=self.master.destroy)        Quit.grid(row=2,column=0)    def say_hi(self):        print(self.TVB1.get())        self.TVB1.set(self.TVE1.get())root = tk.Tk()app = Application(root)app.mainloop()