How to change default background colour in tkinter - python

I want to have all my labels and buttons to have a white background.
I know I can use bg="white" in each one but I was thinking if there was a way I could change the default colour to white or make it so that all widgets have a white background with a single line of code. similar to how you can change the font by doing:
from tkinter import *
window=Tk()
window.option_add("*font",("Arial",15))
window.mainloop()
Which will set all the font to Arial 15

thanks to #acw1668 's comment, can do
window.option_add("*Label*Background", "white")
and
window.option_add("*Button*Background", "white")
to solve it. Just thought id put it as an answer in case people don't see the comment and what not.

Related

How can I make Tkinter Label transparent?

So hello, here I am once again. This time I am having trouble with Label having white background, even though my picture is transparent. How can I make Label background transparent, because background for the label will have gradients.
Here is simple code(Note that green background is going to have some type of gradient in the future):
from tkinter import *
from PIL import ImageTk, Image
root=Tk()
root.geometry("800x800")
canvas=Canvas(root, width=800,height=800)
canvas.create_rectangle(0,0,800,800, fill="green")
canvas.pack()
ClosePhoto = ImageTk.PhotoImage(Image.open('Close_PreHover.png'))
Close=Label(root,image=ClosePhoto)
Close.place(x=350,y=350)
root.mainloop()
Picture used in code
I know about the canvas.create_image() method, but that won't work for me since some of the commands are binded to the label, where my image is, and few of them change the current image with one slightly different. I have tried the attributes() method with -transparent, but I don't really want a whole in the window.
Any help will be of great value, so thank you in advance!
root.attributes("-transparent",color) method not working

Python Tkinter : How to change underground / background color of Treeview, Entry and other widgets

By using wrong keyword I've stupidly struggled to change what we could call the first level of color of ttk widgets. It's not underground or underlayer but fieldbackground as most of you already know.
See below...
A little reminder :
background color is the color under the text in the widget
foreground color is the color of the text
fieldground color is the color of the place where the text will appear
This can be manage with Style:
self.MainTk = tkinter.Tk()
self.style = ttk.Style( self.MainTk )
self.style.configure("Treeview", fieldbackground = 'grey65')
self.style.configure("TEntry", fieldbackground = 'grey65')
Note that some widgets need a T before the name in the configure phase.
see https://www.pythontutorial.net/tkinter/ttk-style/ for more infos.
In Treeview, it manage the Tree mode appearence when you have a single parent collapsed.
From digging old posts it seems that this may not work with some themes under some configuration. You'll find out.
WARNING : Since the error is often made even if it's logic, the widgets must be ttk not tkinter so you must use
self.MyEntry = ttk.Entry(MainTk)
instead of
self.MyEntry = tkinter.Entry(MainTk)
or
self.MyEntry = tk.Entry(MainTk)
if you have import tkinter as tk. Here lie the most common mistake I think.

Python Tkinter checkbutton widget in MacOS: how do I set the text colour

I'm struggling to find a way to change the foreground colour for the checkbutton widget for Tkinter on Mac. This is what I've tried. The foreground colour will always be white no matter what colour i choose.
cbType = Checkbutton(root, text='Series?', background='#d9d9d9', fg='black', width='9').place(x=120, y=199)
I assume this is an issue with Mac and not Tkinter or Python but if anyone has a fix even if it's importing a plugin like how tkmacosx fixes buttons for mac.
Try this:
tk.Checkbutton(root, text='Series?', background='black', fg="red", activebackground="black", selectcolor="black")
It should create a checkbutton that has red text and red check mark but everything else is black. Try changing the colours around.

How to change ttk.progressBar color in python

Does anyone know how I can change the color of my ttk.progressBar? It now shows a green color, and I would love to have it blue.
import ttk
self.progressBar = ttk.Progressbar(frame3, length=560, maximum=100, mode='determinate');
self.progressBar.place(x=-5, y=60)
You can change the color of a progressbar, but it is tricky. First, you need to understand that if you use the default theme, which is the default theme if you do not specify a theme in the tk.style. Then it will pass all the information it needs to the operating system, which will do the drawing using its style, disregarding style info you passed it. meaning it will draw a Window's style green progressbar on Windows and so on and so forth. What you need to do is change the theme to a custom one that ttk draws. Try the "clam" style, it is one of the best looking styles that ttk allows you to chose from. here is a working adapted excerpt from a script I wrote:
import Tkinter as tk
import ttk as ttk
root = tk.Tk()
frame = tk.Frame(root)
frame.grid()
s = ttk.Style()
s.theme_use('clam')
s.configure("red.Horizontal.TProgressbar", foreground='red', background='red')
ttk.Progressbar(frame, style="red.Horizontal.TProgressbar", orient="horizontal",
length=600, mode="determinate", maximum=4, value=1).grid(row=1, column=1)
frame.pack()
and here is a picture confirming it works.
You need to use a style to set the progress bar color. The progress bar style elements are:
Element
Defines
troughcolor
background of full widget
background
background of progress bar
darkcolor
bottom & right progress bar innermost border
lightcolor
top & left bar progress bar innermost border
bordercolor
outline border of the progress bar (outside the above border) and the whole widget
If you want the borders to be the same color as the progress bar, you can use this code:
TROUGH_COLOR = 'blue'
BAR_COLOR = 'green'
style.configure("bar.Horizontal.TProgressbar", troughcolor=TROUGH_COLOR,
bordercolor=TROUGH_COLOR, background=BAR_COLOR, lightcolor=BAR_COLOR,
darkcolor=BAR_COLOR)
I find out that for me it wont work,
I don't want to say this is wrong however
I have found
use [ troughcolor ] to change background and [ background ] for indicator bar
not working for me
s.configure("red.Horizontal.TProgressbar", foreground='red', background='red')
my working way
s.configure("red.Horizontal.TProgressbar", troughcolor ='gray', background='red')
The Progressbar appears to take a style argument. According to the documentation, a style can be used to set the foreground and background colours.
Note: I haven't tried it myself, just pointing you to the relevant docs

Odd Tkinter mouse-over behavior

I've been trying to get a mouse-over event to change the background color of a butten widget in Tkinter. I got some simple code online which works for text, but fails for color. Here it is:
from Tkinter import *
root - Tk()
b = Button(root, text='foo')
b.pack()
def enterB(event):
b.configure(text='bar', background='red')
def leaveB(event):
b.configure(text='foo')
b.bind('<Enter>', enterB)
b.bind('<Leave>', leaveB)
root.mainloop()
When I put my mouse over the button, the text changes to 'bar', but the background color stays gray. When my mouse leaves the area over the button, the background color changes to red, and the text changes to 'foo'. This is the opposite of what should happen.
If I put background='blue' in the b.configure statement in the leaveB function, and leave the rest the same, leaving the button will leave the button blue, and it will never be red.
Can anyone tell me what's happening?
Thanks.
Firstly, I guess that's a typo on line 2, it should be root = Tk()
That program works properly for me, other than the act that on removing the mouse from the button the background stays red. Which can be changed by slightly modifying leaveB function as follows:
def leaveB(event):
b.configure(text="foo", background="SystemButtonFace")
Where "SystemButtonFace" is the default button face color if you are on Windows
I had the same problem (actually I was bothered with the button color not changing after a click unless you left it with the mouse). The solution was to set the activebackground color. In my understanding this is the color which is shown when the mouse is over the button (see http://www.tutorialspoint.com/python/tk_button.htm)
So what I did was:
def enterB(event):
b.configure(text='bar', background='red')
b.configure(activebackground='red');
This way the button already turns red when the mouse is over it. Of course you have to reset the color in the leaveB function to make it change back to grey once you left the button.
If you are on a Mac, you can't change the background color or relief style of a button. You can change the highlightbackground color, however. This is a limitation of tk on macs, thus I would recommend wx instead.

Categories