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()
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=".
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.
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 foreground
option.
You should stop using global imports to eliminate this problem:
import Tkinter as tk
import ttk
...
root = tk.Tk()
...
tk.Button(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With