Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

button1 = Button(root,text='Revert image',foreground = "red",compound="center")

This type of code is not working. it says unknown option "-foreground".

This is the whole code that works -

from Tkinter import *
from ttk import *
def change():
   label.config(text="Hey dude !!")
   label.config(image = img1,background='blue',foreground='yellow')
def click():
         if button1.instate(["disabled"]):
                label.config(image = img1,background='yellow',foreground='green')
                button1.state(['!disabled'])
                button.state(['disabled'])
         else:
                label.config(image = img,background='yellow',foreground='green')
                button1.state(['disabled'])
                button.state(['!disabled'])
root = Tk()
label = Label(root)
img=PhotoImage(file='C:\\Users\\Vivek\\Desktop\\x.gif')
img1= PhotoImage(file='C:\\Users\\Vivek\\Desktop\\y.gif')
img2 = PhotoImage(file='C:\\Users\\Vivek\\Desktop\\z.gif')
button = Button(root)
button.pack()
button1 = Button(root,text='Revert image',compound="center")
img2_small = img2.subsample(30,80)
button.config(image=img2_small,text='Change image',compound='center')
button1.state(["disabled"])
button1.pack()
label.pack()
button.config(command=click)
button1.config(command = click)
label.config(image = img,background='yellow',foreground='green')
label.config(text = 'Hey dude watsup ?? Are you in a need help ?')
label.config(compound = 'left',wraplength=100,font=('Courier',20,'bold'))
label.after(5000,change)
root.mainloop()
like image 244
vivkv Avatar asked Oct 04 '15 17:10

vivkv


People also ask

How can I change the color of text inside a button?

Use a semi-colon to separate the different style elements in the HTML button tag. Type color: in the quotation marks after "style=". This element is used to change the text color in the button. You can place style elements in any order in the quotation markers after "style=".

What do you use to change the back ground color on a widget?

ttk module is used for styling the tkinter widgets such as setting the background color, foreground color, activating the buttons, adding images to labels, justifying the height and width of widgets, etc. In order to add a background color in tkinter widgets, we can specify the background property in the widget.


2 Answers

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

like image 140
Phillip Avatar answered Sep 24 '22 03:09

Phillip


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 tk
import ttk
...
root = tk.Tk()
...
tk.Button(...)
like image 41
Bryan Oakley Avatar answered Sep 26 '22 03:09

Bryan Oakley