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()
Related
How can I remove the dotted black border after a button is clicked in Tkinter (with Ttkthemes)?
I'm on Windows 10, Python 3.7.9.
There seems to be no uniform way to remove it and I searched across Google and SO with no luck. Thanks.
Here's a minimal example:
import tkinter
import tkinter.ttk
from ttkthemes import ThemedTk
tk = ThemedTk(theme="arc")
tk.configure(background="#f5f6f7")
tk.resizable(0,0)
selectFileInput = tkinter.ttk.Button(
tk,
text="Select Input File"
)
selectFileInput.place(x=20,y=60)
tk.mainloop()
I found the solution. It was to create a dummy button and focus it to remove focus from the button to the dummy button by using dummy.focus()
ttk.Button has the keyword argument takefocus this can be set to false and the button does not take focus after it is clicked.
ttk.Button(.., .., takefocus=False)
Therefore you do not need a hack with a dummy button that takes focus as in the answer.
This code is a tutorial from youtube. The given code is supposed to print right on the right click of the mouse on the console and same for the left click but it is not doing so. I think the problem is with the bind function.
I am using python 3.7 which already have tkinter package in it, what can I do to make it work, thank you very much.
from tkinter import *
root = Tk()
def leftclick(event):
print("left")
def rightclick(event):
print("right")
frame = Frame(root, width=300, height=300)
frame.bind("button-1", leftclick)
frame.bind("button-2", rightclick)
frame.pack()
root.mainloop()
I expect the program to print 'left' in the console on the left click of the mouse inside the tk window and same for right click
First, as already noted in comments, the mouse button events need <...>. About the rightclick not working: This is because the even for the right mouse button is actually <Button-3>, whereas <Button-2> is the middle mouse button (or pressing down on the mouse wheel).
frame.bind("<Button-1>", leftclick)
frame.bind("<Button-3>", rightclick)
This may be a bit unintuitive if you think of the right mouse button as the "secondary" button, but makes sense if you just enumerate the buttons from left to right. This is AFAIK also consistent with all (most?) other UI frameworks and languages.
In my pyqt gui while a button is pressed the button's text color changes. And when the button is released the color goes back to its original self.
I have the code working like the following:
self.ui.pButton_save.pressed.connect(self.save_pressed)
self.ui.pButton_cancel.pressed.connect(self.cancel_pressed)
self.ui.pButton_save.released.connect(self.save_released)
self.ui.pButton_cancel.released.connect(self.cancel_released)
def save_pressed(self):
self.ui.pButton_save.setStyleSheet("color: white")
def cancel_pressed(self):
self.ui.pButton_cancel.setStyleSheet("color: white")
def save_released(self):
self.ui.pButton_save.setStyleSheet("color: green")
def cancel_released(self):
self.ui.pButton_cancel.setStyleSheet("color: red")
The code works fine. But as you can see there are so many lines for this simple task. Probably there is a cleaner (more Pythonic) way of doing it. Any advice?
This is the way to do it with Qt Style Sheet, the ones that handle the states like the pressed button.
self.ui.pButton_save.setStyleSheet(
"QPushButton:pressed{color: white} QPushButton{color: green}")
self.ui.pButton_cancel.setStyleSheet(
"QPushButton:pressed{color: white} QPushButton{color: red}")
I am trying to make a kind of movable widget program in python with Tkinter, but I ran into a problem. I can't detect a click without interfering with the function of the widget that you click on. (example: Text or Button widget)
Here is an example:
import tkinter as tk
main = tk.Tk()
notes = tk.Text(main, height = 15, bd = 4)
notes.place(y = 10, x = 20)
notes.bind("<Button-1>", lambda event: print("hello"))
But if you try and click in the middle, it still works. Is there any way to make it only clickable on the border and not the widget itself?
You can prevent the default behavior by returning "break" from the event handler. For example:
def hello(event):
print("hello")
return "break"
...
notes.bind("<Button-1>", hello)
The same works for any widget. This prevents the default behavior (moving the cursor, clicking the button) from happening.
Another choice is to put each widget in a frame with a small border, and then put the binding on the frame.
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.