如何在按下后更改按钮的bg颜色并返回到先前的颜色 [英] how to change bg color of button after pressing and get back to previous color
本文介绍了如何在按下后更改按钮的bg颜色并返回到先前的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的程序中,我想在按下按钮后将按钮的背景色更改为绿色,并且更改后的颜色应保留2秒钟,然后再更改回原始颜色.有办法吗?
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()
程序中默认颜色为黑色,现在我想在按下后更改为绿色2秒钟
In the program the default color is black now i want to change to green for 2secs after pressing
推荐答案
您可以使用 after
方法来安排函数在预定的时间后运行.您可以使用 configure
方法来更改窗口小部件选项.将这两个放在一起,您将得到以下内容:
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")
这篇关于如何在按下后更改按钮的bg颜色并返回到先前的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文