如何限制次数地闪烁 tkinter 标签,并且仅在满足条件时
How to flash tkinter label limited number of times, and only if conditions are met
我有 运行 这个答案中给出的闪烁 tkinter 标签的好例子:Flashing Tkinter Labels
但是,当我尝试为其添加一些复杂性时,它失败了。
首先,我需要它只在满足某些条件时才闪烁(这是一个警报,所以它应该只在警报状态下闪烁,而不是在正常情况下闪烁)。
这是我想要做的事情的基本版本。
import Tkinter as tk
import tkFont
import epics
global root
class AlarmGUI:
def __init__(self,parent):
self.ending = False
self.crhl = tk.Label(text='CFHT RH',bg='light blue')
self.crhl.grid(row=1, column=2,
padx=20, pady=20, sticky=tk.W)
self.crhw = tk.Label(text='something',font=("Helvetica", 25,"bold"),bg='light blue')
self.crhw.grid(row=2,column=0, sticky=tk.W)
self.cfht()
def flash(self):
bg = self.crhw.cget('background')
fg = self.crhw.cget('foreground')
self.crhw.configure(background=fg,foreground=bg)
self.crhw.after(1000,self.flash)
def cfht(self):
#This reads in the value that is being tested
crh = epics.caget('ws:wsHumid') #Read in CFHT Relative Humidity
#Here, I display the value 'crh'
self.crhw.grid(row=2, column=2,sticky=tk.W+tk.E+tk.N+tk.S)
self.crhw.configure(text=crh,fg='Red',bg='Gray')
#Now I need to determine if the value crh is in an alarm state
if (crh > 85): #If the value is over 85, I need the label to flash.
self.crhw.flash()
#Then this keeps checking the humidity value
self.crhw.after(30000,cfht)
def main():
global root
root = tk.Tk()
gui = AlarmGUI(root)
root.mainloop()
if __name__ == '__main__':
main()
我也试过让闪光功能只闪光一定次数。当我说这个(下面)时,它不会闪烁。它只打印到屏幕 30 次,然后在 gui 出现在屏幕上之前需要大约 30 秒,并且不会闪烁:
def flash(self,count):
bg = self.crhw.cget('background')
fg = self.crhw.cget('foreground')
self.crhw.configure(background=fg,foreground=bg)
count +=1
if (count < 31):
print count, bg, fg
self.crhw.after(1000,self.flash(count))
#And I call it with something like
self.flash(0) #to initialize the count
你们很亲近。您需要记住 after
需要 reference 函数。当您执行类似 after(..., self.flash(count))
的操作时,您是在调用 after
之前 调用 函数。或者,更准确地说,您快速连续调用它 31 次,每次将结果 (None
) 提供给 after
,以创建 31 个什么都不做的作业。
after
方便地允许您在调用的内容中包含其他参数:
self.crhw.after(1000, self.flash, count)
面向对象的方法
这是使用 python 的面向对象特性的最佳时机。您可以创建自己的带有 flash
方法的标签 class,这样您就不必将闪烁代码添加到您的主应用程序中。这也让您可以轻松拥有任意数量的闪烁标签。
class FlashableLabel(tk.Label):
def flash(self,count):
bg = self.cget('background')
fg = self.cget('foreground')
self.configure(background=fg,foreground=bg)
count +=1
if (count < 31):
self.after(1000,self.flash, count)
您可以像正常使用它一样使用它 Label
:
self.crhw = FlashableLabel(...)
你可以这样刷:
self.crhw.flash(0)
我有 运行 这个答案中给出的闪烁 tkinter 标签的好例子:Flashing Tkinter Labels
但是,当我尝试为其添加一些复杂性时,它失败了。
首先,我需要它只在满足某些条件时才闪烁(这是一个警报,所以它应该只在警报状态下闪烁,而不是在正常情况下闪烁)。
这是我想要做的事情的基本版本。
import Tkinter as tk
import tkFont
import epics
global root
class AlarmGUI:
def __init__(self,parent):
self.ending = False
self.crhl = tk.Label(text='CFHT RH',bg='light blue')
self.crhl.grid(row=1, column=2,
padx=20, pady=20, sticky=tk.W)
self.crhw = tk.Label(text='something',font=("Helvetica", 25,"bold"),bg='light blue')
self.crhw.grid(row=2,column=0, sticky=tk.W)
self.cfht()
def flash(self):
bg = self.crhw.cget('background')
fg = self.crhw.cget('foreground')
self.crhw.configure(background=fg,foreground=bg)
self.crhw.after(1000,self.flash)
def cfht(self):
#This reads in the value that is being tested
crh = epics.caget('ws:wsHumid') #Read in CFHT Relative Humidity
#Here, I display the value 'crh'
self.crhw.grid(row=2, column=2,sticky=tk.W+tk.E+tk.N+tk.S)
self.crhw.configure(text=crh,fg='Red',bg='Gray')
#Now I need to determine if the value crh is in an alarm state
if (crh > 85): #If the value is over 85, I need the label to flash.
self.crhw.flash()
#Then this keeps checking the humidity value
self.crhw.after(30000,cfht)
def main():
global root
root = tk.Tk()
gui = AlarmGUI(root)
root.mainloop()
if __name__ == '__main__':
main()
我也试过让闪光功能只闪光一定次数。当我说这个(下面)时,它不会闪烁。它只打印到屏幕 30 次,然后在 gui 出现在屏幕上之前需要大约 30 秒,并且不会闪烁:
def flash(self,count):
bg = self.crhw.cget('background')
fg = self.crhw.cget('foreground')
self.crhw.configure(background=fg,foreground=bg)
count +=1
if (count < 31):
print count, bg, fg
self.crhw.after(1000,self.flash(count))
#And I call it with something like
self.flash(0) #to initialize the count
你们很亲近。您需要记住 after
需要 reference 函数。当您执行类似 after(..., self.flash(count))
的操作时,您是在调用 after
之前 调用 函数。或者,更准确地说,您快速连续调用它 31 次,每次将结果 (None
) 提供给 after
,以创建 31 个什么都不做的作业。
after
方便地允许您在调用的内容中包含其他参数:
self.crhw.after(1000, self.flash, count)
面向对象的方法
这是使用 python 的面向对象特性的最佳时机。您可以创建自己的带有 flash
方法的标签 class,这样您就不必将闪烁代码添加到您的主应用程序中。这也让您可以轻松拥有任意数量的闪烁标签。
class FlashableLabel(tk.Label):
def flash(self,count):
bg = self.cget('background')
fg = self.cget('foreground')
self.configure(background=fg,foreground=bg)
count +=1
if (count < 31):
self.after(1000,self.flash, count)
您可以像正常使用它一样使用它 Label
:
self.crhw = FlashableLabel(...)
你可以这样刷:
self.crhw.flash(0)