[英]how to stop blinking of text label when button is pressed?
I am created two button and two text label and my application is when "1" is pressed "hello" should start blinking and when "2" is pressed "hi" should start blinking and "hello" should stop blinking.it is a demo code of my main project .我创建了两个按钮和两个文本标签,我的应用程序是当按下“1”时“hello”应该开始闪烁,当按下“2”时“hi”应该开始闪烁并且“hello”应该停止闪烁。这是一个演示我的主要项目的代码。 Here is my code.这是我的代码。
import tkinter as Tk
flash_delay = 500 # msec between colour change
flash_colours = ('white', 'red') # Two colours to swap between
def flashColour(object, colour_index):
object.config(background = flash_colours[colour_index])
root.after(flash_delay, flashColour, object, 1 - colour_index)
def flashColour1(object, colour_index):
root.txt.config(bg="white")
object.config(background=flash_colours[colour_index])
root.after(flash_delay, flashColour1, object, 1 - colour_index)
root = Tk.Tk()
root.geometry("100x100")
root.txt = Tk.Text(root, height=2,width=10,font=("TimesNewroman",7,'bold'),
background = flash_colours[0])
root.txt.pack()
root.txt.insert(Tk.END,"hello")
root.txt1 = Tk.Text(root, height=2,width=10,font=("TimesNewroman",7,'bold'),
background = flash_colours[0])
root.txt1.pack()
root.txt1.insert(Tk.END,"hi")
root.button1=Tk.Button(root,text="1",command = lambda: flashColour(root.txt, 0))
root.button1.pack()
root.button2=Tk.Button(root,text="2",command = lambda: flashColour1(root.txt1, 0))
root.button2.pack()
root.mainloop()
Try this... It will work试试这个......它会起作用
import tkinter as Tk
flash_delay = 500 # msec between colour change
flash_colours = ('white', 'red') # Two colours to swap between
def flashColour(object, colour_index):
global a
object.config(background=flash_colours[colour_index])
a = root.after(flash_delay, flashColour, object, 1 - colour_index)
def flashColour1(object, colour_index):
global b
root.txt.config(bg="white")
object.config(background=flash_colours[colour_index])
b = root.after(flash_delay, flashColour1, object, 1 - colour_index)
def stopFlash(object, colour_index):
root.txt.config(bg="white")
object.config(background=flash_colours[colour_index])
root.after_cancel(a)
def stopFlash1(object, colour_index):
root.txt.config(bg="white")
object.config(background=flash_colours[colour_index])
root.after_cancel(b)
root = Tk.Tk()
root.geometry("100x160")
root.txt = Tk.Text(root, height=2, width=10, font=("TimesNewroman", 7, 'bold'),
background=flash_colours[0])
root.txt.pack()
root.txt.insert(Tk.END, "hello")
root.txt1 = Tk.Text(root, height=2, width=10, font=("TimesNewroman", 7, 'bold'),
background=flash_colours[0])
root.txt1.pack()
root.txt1.insert(Tk.END, "hi")
root.button1 = Tk.Button(root, text="1", command=lambda: flashColour(root.txt, 0))
root.button1.pack()
root.button2 = Tk.Button(root, text="2", command=lambda: flashColour1(root.txt1, 0))
root.button2.pack()
root.button3 = Tk.Button(root, text="Stop1", command=lambda: stopFlash(root.txt, 0))
root.button3.pack()
root.button4 = Tk.Button(root, text="Stop2", command=lambda: stopFlash1(root.txt1, 0))
root.button4.pack()
root.mainloop()
Used root.after_cancel(after_id)
to cancel the effect of after !使用root.after_cancel(after_id)
取消 after 的效果!
Just save the ID of the after(...)
task and use after_cancel(...)
to cancel the scheduled task:只需保存after(...)
任务的 ID 并使用after_cancel(...)
取消计划任务:
def flashColour(object, colour_index):
object.config(background = flash_colours[colour_index])
flashColour.after_id = root.after(flash_delay, flashColour, object, 1 - colour_index)
def flashColour1(object, colour_index):
if hasattr(flashColour, 'after_id'):
root.after_cancel(flashColour.after_id)
root.txt.config(bg="white")
object.config(background=flash_colours[colour_index])
root.after(flash_delay, flashColour1, object, 1 - colour_index)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.