wxpython - How to get a blinking label on the basis of random value input in a GUI? -
here code:
import wx import random import time class mypanel(wx.panel): """"""
----------------------------------------------------------------------
def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) self.font = wx.font(12, wx.default, wx.normal, wx.normal) self.flashingtext = wx.statictext(self, label="battery") self.flashingtext.setfont(self.font) self.timer = wx.timer(self) self.bind(wx.evt_timer, self.update, self.timer) self.timer.start(2000) #---------------------------------------------------------------------- def update(self, event): """""" x=random.uniform(1,10) print(x) if x>5: self.flashingtext.setbackgroundcolour('red') else: self.flashingtext.setbackgroundcolour('white')
# class myframe(wx.frame): """ok""" #---------------------------------------------------------------------- def __init__(self): """constructor""" wx.frame.__init__(self, none, title="flashing text!") panel = mypanel(self) self.show()
----------------------------------------------------------------------
if __name__ == "__main__": app = wx.app(false) frame = myframe() time.sleep(2) app.mainloop()
the code not working though logic seems sound. please correct if could!
ps: please bear me, beginner wxpython , gui-level programming.
add self.refresh() end of update method statictext update background colour
import wx import random class mypanel(wx.panel): """""" def __init__(self, parent): """constructor""" wx.panel.__init__(self, parent) self.font = wx.font(12, wx.default, wx.normal, wx.normal) self.flashingtext = wx.statictext(self, label="battery") self.flashingtext.setfont(self.font) self.timer = wx.timer(self) self.bind(wx.evt_timer, self.update, self.timer) self.timer.start(2000) def update(self, event): """""" x = random.uniform(1, 10) print(x) if x > 5: self.flashingtext.setbackgroundcolour('red') else: self.flashingtext.setbackgroundcolour('white') self.refresh() class myframe(wx.frame): """ok""" def __init__(self): """constructor""" wx.frame.__init__(self, none, title="flashing text!") panel = mypanel(self) self.show() if __name__ == "__main__": app = wx.app(false) frame = myframe() app.mainloop()
Comments
Post a Comment