0

I got some helpful code from GeeksForGeeks and modified it slightly. It colors the text, but no, I wanted something different: I wanted to make the background blue. I tried what I hoped would work, but no, background means "what it looks like when the window is in the background". And anyway, it doesn't work, text just becomes black when the window is in the background. I have looked through the documentation, all I could find, and there is no clear reference. Is this doable?

from tkinter import *
from tkinter.ttk import *
 
# Create Object
root = Tk()
root.geometry('1000x600')
 
style = Style()
style.configure('W.TButton', font =
               ('calibri', 10, 'bold', 'underline'),
                foreground = 'red', background = 'green')
 
''' Button 1'''
btn1 = Button(root, text = 'Quit !',
                style = 'W.TButton',
             command = root.destroy)
btn1.grid(row = 0, column = 3, padx = 100)
 
''' Button 2'''
btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
 
root.mainloop()

enter image description here

JRiggles
  • 4,847
  • 1
  • 12
  • 27
Joymaker
  • 813
  • 1
  • 9
  • 23

1 Answers1

0

this bug is a known bug in tkinter.ttk, it is caused by the default theme, "aqua". this theme doesn't respect backgroundcolors. if you want to set a background color, you should change the theme of the style. that you can do by adding style.theme_use("classic"). then your code become:

from tkinter import *
from tkinter.ttk import *

# Create Object
root = Tk()
root.geometry('1000x600')

style = Style(root)
style.theme_use("classic")  # only this piece of code is new
style.configure('W.TButton', font=
('calibri', 10, 'bold', 'underline'),
                foreground='red', background='green')

''' Button 1'''
btn1 = Button(root, text='Quit !',
              style='W.TButton',
              command=root.destroy)
btn1.grid(row=0, column=3, padx=100)

''' Button 2'''
btn2 = Button(root, text='Click me !', command=None)
btn2.grid(row=1, column=3, pady=10, padx=100)

root.mainloop()

on my computer, the buttons become ugly, but you can also use a label and bind to <Button-1>.

i hope this helped!