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.
Related
I am making a simple application with 1 button and 1 label where pressing the button changes the text on the label. Both label and button and placed using the tkinter's grid system however when I press the button the label's text changes size as expected but the button becomes stretched to the label's length too. Why?
This is my code.
import tkinter as tk
def change():
label1.config(text = "example text that is really big which shows the button stretching")
window = tk.Tk()
window.config(bg="black")
b1=tk.Button(window,text="button1",font=("Segoe UI",40),command=change).grid(row=1,column=1,sticky="news")
label1=tk.Label(text = "text",font=("Segoe UI",20),bg="black",fg="white")
label1.grid(row=2,column=1,sticky="news")
window.mainloop()
The expected result was the label changing and the button's size staying the same but instead the button becomes stretched to the labels length. I have tried a lot of things trying to make this work but I can't figure it out.
Before pressing the button
After pressing the button
Setting sticky="news" for the button, will expand the button to fill the available space in the four directions North, East, West, South. Try changing it to sticky="w" to make it stick to the West. So, the line of code for creating the button will become:
b1=tk.Button(window,text="button1",font=("Segoe UI",40),command=change).grid(row=1,column=1,sticky="w")
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.
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()
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)
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):