tkinter: button color does not update - python

The button3 of my GUI is calling a function which takes quite long to calculate stuff. So in the meantime I want to change the buttons text and color:
self.button3.config(foreground='red')
self.button3['text'] = 'PLEASE WAIT ...'
self.button3.update_idletasks()
The text of the button does indeed change, but the color stays the same. Why?

Your button probably remains in its active state during the long calculation. So you may want to set its activeforeground color to red:
self.button3.config(activeforeground='red')
activeforeground = What foreground color to use when the button is
active. The default is system specific. (activeForeground/Background)
(Tkinter Button documentation)

Related

Default color of button Tkinter

What is the default button color's name? The color that appears if you don't enter any color like in bg. I want to delete the bg I entered during the program to the default color so it will look like it did in the beginning.
Thanks!
As #jasonharper wrote in the comments. Since the default color is different between operating systems, the easiest way to reset the button color is to fetch and store it before setting it to some other color. Then you could simply use this value to reset the button to its default color.
import tkinter
root = tkinter.Tk()
button = tkinter.Button(root, text='Hello')
DEFAULT_BUTTON_COLOR = button['bg']
button['bg'] = 'blue'
button.pack()
# Somewhere in your code where you want to reset the color
button['bg'] = DEFAULT_BUTTON_COLOR

How to change the color of the active background of the dropdown menu (i.e. tk.Listbox) in a ttk.Combobox

The following code creates a ttk.Combobox widget:
import tkinter.ttk as ttk
default_values = ['Peter','Scotty','Walter','Scott','Mary','Sarah','Jane',
'Oscar','Walley','Faith','Bill','Egor','Heley']
s=ttk.Style()
s.configure( 'TCombobox', font=('Purisa', 20, 'bold'), background ='cyan',
fieldbackground='pink')
c=ttk.Combobox(values=default_values)
c.master.option_add( '*TCombobox*Listbox.background', 'yellow')
c.master.option_add( '*TCombobox*Listbox.selectbackground','red') #does not work
c.master.option_add( '*TCombobox*Listbox.selectforeground','grey') #does not work
c.master.option_add( '*TCombobox*Listbox.highlightbackground','blue') # does not work
c.master.option_add( '*TCombobox*Listbox.highlightforeground','green') #does not work
c.master.option_add( '*TCombobox*Listbox.activestyle', 'underline') #does not work
c.grid()
After clicking on the Combobox downarrow, a dropdown menu (which is a tk.Listbox widget) containing the default values will appear. When the mouse hovers over the dropdown menu, an active background below the mouse pointer will appear. I would like to change the color of this grey active background. How do I do so?
As the dropdown menu isn't a ttk widget, it will not respond to and ttk.Style() settings. I have also tried the .option_add method but only the Listbox background could be changed.
Issue:
You are trying to use the correct option to make the background color change:
c.master.option_add( '*TCombobox*Listbox.selectbackground','red')
But you made a little mistake i.e. selectbackground. In selectbackground , the first letter of background should be capital.
*TCombobox*Listbox.selectbackground becomes *TCombobox*Listbox.selectBackground
-----------------------__^__-----------------------------------------__^__-------
Try:
c.master.option_add( '*TCombobox*Listbox.selectBackground','red')
Similarly , foreground becomes Foreground.

Button momentarily changes color when selected tkinter

Is there a best practice for making a tkinter button momentarily change color when it is selected (so the user gets a visual feedback that the button was pressed).
I have read it is not a good idea to use time.sleep() in a tkinter GUI.
When my button is pressed, the code happens so fast that even when I have a button.config() command to change color, it doesn't occur without using time.sleep()
Any suggestions?
I think this may be what you want:
Button(background=normal_color, foreground=text_color,
activebackground=pressed_color, activeforeground=pressed_text_color)
This changes the button from normal_color to pressed_color when the button is pressed.
It's actually a simple question with a simple answer but I had to look everywhere too. Finally found this answer by reading http://effbot.org/tkinterbook/button.htm.
You can change the color upon clicking, then use the after method to reset the color back to the original, after some time has passed
import tkinter as tk
def reset_color():
bt['fg'] = 'black'
def clickme():
print('clicked')
bt['fg'] = 'red'
root.after(2000, reset_color) # after 2 seconds
root = tk.Tk()
bt = tk.Button(root, text='will color for a while\nafter clicked', command=clickme)
bt.pack()
root.mainloop()

Dot in a selected radiobutton with bg, fg specified disappears

Beginner question: I'm trying to change the colour of my GUI, particularly the radiobuttons. I need inverted colours, so black background, white text.
self.radiobuttonVariable = Tkinter.IntVar()
radiobutton1 = Tkinter.Radiobutton(self, text=u'E', variable = self.radiobuttonVariable,
bg='black', fg='white' activebackground='black', activeforeground='white',
value = 1, command = self.RadioSelect)
radiobutton1.grid(column=2, row=1, sticky='ES')
This looks just fine, but when I press the button, it's the dot is there only as long I'm pressing it, disappears as soon as I let it go. The variable doesn't change, it stays on the right value, just that the dot disappears. No issue whatsoever when I remove the colour-management options. Any ideas?
As far as I can tell, specifying the foreground (presumably for the text) also sets the selected "dot color". You can set the selectcolor attribute of the element to color the "background" of the radiobutton so that you can see the white dot.
For example, selectcolor='red' in Windows:
Be aware that the manpage indicates that coloring all radio buttons with selectcolor (instead of only the selected button) may be Windows-only:
Under Windows, this color is used as the background for the indicator regardless of the select state.
That being said, I got the same effect in linux under Python 2.7 and 3.3:
I just used red to distinguish the part of the widget that selectcolor affected, you'd probably want selectcolor='black' or something a bit lighter to show the depression selectcolor='#222222' (below):

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