12672

how to change bg color of button after pressing and get back to previous color

Question:

In the below program i want to change the background color of the button to green after pressing it and changed color should remain for 2seconds and later change back to original color. Is there a way to do?

import tkinter as tk class MyGui(tk.Tk): def __init__(self): super(MyGui, self).__init__() self.create_widgets() def widget_button(self): self.frame = tk.Frame(self) self.frame.pack() self.buttonA = tk.Button(self.frame, padx=13, pady=6, bd=4, text="Sample",bg="black", command=self.trial) def trial(self): print("Button color is changed to green") if __name__ == "__main__": root = MyGui() root.mainloop()

In the program the default color is black now i want to change to green for 2secs after pressing

Answer1:

You can use the after method to schedule a function to run after a pre-defined amount of time. You can use the configure method to change a widget option. Put those two together and you have something like this:

def trial(self): self.buttonA.configure(background="green") self.after(2000, lambda: self.buttonA.configure(background="black")

Answer2:

it works fine if i put code like:

def trial(self): self.buttonA.configure(background="green") self.after(10, lambda: self.buttonA.configure(background="black") self.after(2000, lambda: self.buttonA.configure(background="green")

But am not sure whether its correct way or not

Recommend

  • Using StringVar data as a list
  • tkinter button in class can't call function
  • Python 3.4 tkinter checkbutton variable handling not working / responding
  • How to speed up tkinter embedded matplot lib and python
  • Tkinter doesn't work with Python 3.5
  • How to loop this code?
  • Tkinter Frame container color not visible
  • Printing the output of a script to a window in python
  • Python 2.7.2 global variables + tkinter
  • Can't get the function to work with checkbuttons using tkinter
  • how can you display an image on a gui using tk in python2.7
  • 'adding' two angles
  • Dynamically adding a property to a class
  • Send Kubernetes cluster logs to AWS Elasticsearch
  • Java (Robot) screenshot beyond visible
  • tcltk in portable R
  • What should I use to create a Windows GUI?
  • Sending a variable with a button-command (python 3.3 / tkinter) [duplicate]
  • Centering a Tkinter Toplevel window in both windows and remote X11?
  • How to display rendered html content in text widget of tkinter in python 3.4.x
  • Taking input from user and returning an answer in TKinter
  • Python : How to center Label in tkinter window
  • resize image using tkinter
  • Python Tkinter after event OOPS implementation
  • MRO with multiple inheritance in python
  • Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • wxPython: displaying multiple widgets in same frame
  • how to avoid repetitive constructor in children
  • error importing numpy
  • Grails calculated field in SQL
  • R - Combining Columns to String Based on Logical Match
  • Deleting and Updating values from a cusrsor adapter
  • How to add date and time under each post in guestbook in google app engine
  • Possible to stop flickering java tooltip in heavyweight mode?
  • JSON with duplicate key names losing information when parsed
  • Return words with double consecutive letters
  • embed rChart in Markdown
  • Unable to use reactive element in my shiny app