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
Related
I'm having difficulty figuring out how to change the colour of a button in python using tkinter. All the tutorials I've seen show how to change the colour while the button is pressed.
For instance
from tkinter import *
root = Tk()
root.title('Testing')
root.geometry("400x400")
btn = Button(root, text = "Change my colour", bg="#00FF00", activebackground="#FF0000")
btn.pack()
root.mainloop()
is my current code and changes the colour from green to red when pressed, but as soon as you lift the mouse it goes back to green. What I'm looking for is how to keep the button red after it has been clicked, and then change it back when it is clicked again.
I appreciate any help or advice, thank you!
Probably the easiest way to implement a dual colored button is to use a Checkbutton. Try this.
tk.Checkbutton(root, indicatoron = 0, activebackground = 'black', background = "green", selectcolor = "red", text = "Button", foreground = "yellow").pack()
Note the use of Checkbutton indicator = 0 to make it look like a standard button object.
I'm designing a GUI application using Tkinter and for this project, I need buttons for the menu. While looking into the buttons I wasn't blown away by the customization options that come with the buttons, especially when I found out that you can bind click arguments to rectangles.
This allows me to customize the "button" in (almost) limitless ways, but to allow me to put text on the button I need to create a rectangle element and a text element and bind them together using Tkinter's tag_bind property.
One of the design properties of the button that I wanted was active fill when the user moused over the element. Right now I'm just using activefill="" which works, except the text element and the button element will only fill while the mouse is over that element. So, for example, when I mouse over the button the button excluding the text will highlight and vise versa when I mouse over the text.
Below is a simplified (for brevity) version of what I use to generate the buttons;
button = canvas.create_rectangle(button_width, button_height, 10, 10, fill="000", activefill="111", tags="test")
text = canvas.create_text((button_width/2), (button_height/2), activefill="111", tags="test")
canvas.tag_bind("test", "<Button-1>", "foo")
Is there a way to bind the active fill function to a tag rather than a specific element?
Another option is that I completely missed a bunch of information about customizing the buttons in Tkinter, and I would not be apposed to learning about that.
Option 1
I would personally not go for the presented solution. I do not know if you are using the button provided by tk or ttk. But, with the tkinter.tk, you could absolutely change the appearance of the button.
Following, I give you an example that produces a button with the following characteristics:
Blue foreground
Flat appearance
When hovered, the background is green
When pressed, the background is red
The code is as follows:
import tkinter as tk
root = tk.Tk()
# Function hovering
def on_enter(e):
btn['background'] = 'green'
def on_leave(e):
btn['background'] = 'white'
# Create the button
btn = tk.Button(root, background='white', activebackground='red', foreground='blue',relief='flat',text='Test',width=20)
btn.pack()
# Bindings
btn.bind("<Enter>", on_enter)
btn.bind("<Leave>", on_leave)
# Loop
root.mainloop()
Option 2
If even after having tried the tk.Button, you are not glad with the result, I would create a Frame containing a Label (you can do nearly anything with that combination). Then, you could change the background of the frame according to any user action, like:
import tkinter as tk
root = tk.Tk()
# Function hovering
def on_enter(e):
lab['background'] = 'green'
def on_leave(e):
lab['background'] = 'white'
# Click
def on_click(e):
print("hi")
# Create the frame with a label inside
fr = tk.Frame(root)
lab = tk.Label(fr, text="Test", width=20, background="white")
# Packing
fr.pack()
lab.pack()
# Bindings
fr.bind("<Enter>", on_enter)
fr.bind("<Leave>", on_leave)
lab.bind("<Button-1>", on_click)
# Loop
root.mainloop()
You could even create a class with the above combination.
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.
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)
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.