Active text rather than static text (Phoenix)

Keywords : wx.StaticText, Active text, Custom.


Demonstrating :

Tested py3.8.10, wx4.x and Linux.

Tested py3.10.5, wx4.2.1 and Win11.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


First example

Latest version here : https://discuss.wxpython.org/t/activetext-rather-than-statictext/36370

img_sample_one.png

   1 import wx
   2 '''
   3     ActiveText.py
   4 
   5     A drop-in replacement for wx.StaticText with added functionality.
   6     Envisaged originally to be used for a generic messaging area, where you want to inform the user
   7      about what is happening, but don't want the message left cluttering up the screen.
   8 
   9     It's StaticText Jim, but not as we know it!
  10     Not exactly singing and dancing but at least self-clearing, sliding left and right, flashing and scrolling
  11     Can still be used as a standard statictext, simply by using SetLabel()
  12 
  13     Timers for Clearing, Flashing, Cycling and Tickering
  14     Clearing options:   Instant, Left to Right or Right to Left
  15     Flasing options:    Constant, Initially or Finally
  16     Tickering options:  Left to Right or Right to Left, limited repetition or continuous
  17 
  18     A sub-classed StaticText module provides:
  19         self Clearing text, after a period in milliseconds
  20         a Flash function, to flash text every n milliseconds, with optional timeout
  21          both functions can be used at the same time, giving Flashing, Clearing text
  22         a Cycling option for text to be re-presented, a given period after it has been cleared
  23          Flashing is not an option with Cycling text, for re-presented text
  24          Cycling can be combined with directional Clearing
  25         a Tickering option using a fixed width font, where text runs across the screen to left or right
  26 
  27     Flashing can be constant, for an initial period of time or for a period time before text is cleared
  28 
  29     Clearing can be instant, Right to Left or Left to Right
  30 
  31     Tickering can repeat a fixed number of times or be continuous
  32 
  33     The simple News-ticker style uses a StaticText rather than a DC as used by wx.lib.ticker
  34     It attempts to force a tolerably stable label size, by using a fixed width font,
  35      although this can be overridden, results are unpredictable.
  36      A width should always be set with size=(width, height)i.e. (300, -1) when creating the control
  37      for use with the Ticker style or specify the maximum number of characters to be used for the scrolling area.
  38     The ticker is used to present text in a area, smaller than space allows for.
  39 
  40     Setting a new label (Clearing/Cycling or Tickering) will cancel any existing attributes
  41 
  42     Adds new functions to a standard StaticText:
  43 
  44         CycleStop()          Stops the cycler
  45 
  46         Deactivate()         Stop all timers
  47 
  48         GetClearPeriod()     returns Clearing period (ms)
  49 
  50         GetCyclePeriod()     returns cycling period (ms)
  51 
  52         GetFlashCountdown()  returns live calculated time before flashing starts or None
  53 
  54         GetFlashDelay()      returns flash delay in ms
  55 
  56         GetFlashPeriod()     returns period (ms)
  57 
  58         IsActive()           Is the control Flashing, Clearing, Cycling or Tickering returns True or False
  59 
  60         IsClearing()         returns True or False
  61                              either actively clearing or set to clear
  62 
  63         IsCycling()          returns True or False
  64 
  65         IsFlashing()         returns True or False
  66 
  67         IsTickering()        returns True or False
  68 
  69         SetClearLabel(label, period=milliseconds)
  70                                - Used in place of wx.StaticText.SetLabel(self,label)
  71                                  Sets the Label and starts the timer using the value
  72                                   set by SetTimer() or optional period parameter
  73                                  Subsequent calls override existing settings
  74                                  A period of < 0 turns off the clearing function
  75                                  A call with a period > 0 resets the timer period,
  76                                   which will be used by subsequent calls without a period parameter
  77                                  A period of None turns off the clearing function for this occurrence only
  78                                  (Leaves the standard SetLabel unaffected)
  79                                  Default label='' period = -1
  80 
  81         SetClearStyle(style=None, period=milliseconds)
  82                                 Style set to None (default) when text is cleared it's instantly
  83                                 Style = "ltr" (left to right) texted is cleared 1 character at a time
  84                                  from left to right
  85                                 Style = "rtl" (right to left) texted is cleared 1 character at a time
  86                                  from right to left (best if using wx.ALIGN_RIGHT for the text style)
  87                                 Period in milliseconds (default = 50)
  88                                  Time between each character being removed.
  89 
  90         SetCycleLabel(label, cycle=-1)
  91                                - Used in place of wx.StaticText.SetLabel(self,label)
  92                                  Sets the Label and starts the clearing timer using the value set by SetTimer()
  93                                  cycle = the amount of time to wait in ms before re-presenting the text after clearing
  94                                  A cycle of < 0 turns off the cycling function
  95 
  96         SetFlash(period=milliseconds, timeout=milliseconds)
  97                                - Starts/Stops the Flasher flashes every period
  98                                   a period of 100 = 10 times a second
  99                                  Call with period <= 0 to deactivate the flasher
 100                                   the flasher will endevour to leave the last good label showing i.e. not equal to spaces
 101                                  Call with period > 0 to activate the flasher using the period
 102                                  Call with period > 0 and timeout > 0 to activate the flasher for timeout ms
 103                                   a period of 250 and a timeout of 5000 will flash 4 times a second for 5 seconds
 104                                  Call with period > 0 and timeout < 0 to delay the flasher for timeout ms
 105                                   before a text Clear
 106                                   a negative timeout will start the flasher timeout ms before the text is cleared
 107                                  On Call any pre-existing Calllater for timeout cancellation or delayed flash is terminated
 108                                  The flasher uses GetLabel not GetLabelText
 109                                  Default period -1 milliseconds
 110                                  Default timeout None
 111 
 112         SetTickerLabel(label, period=milliseconds, direction='ltr', maxchars=-1, repeat=-1, facename=None)
 113                                - Used in place of wx.StaticText.SetLabel(self,label)
 114                                  Sets the Label and starts the timer
 115                                   period is the number of milliseconds between each tick left or right
 116                                   direction 'ltr' text moves Left to right
 117                                   direction 'rtl' text move right to left
 118                                   maxchars overrides any size stipulation, to provide a manual definition
 119                                    of how big the scrolling area is in characters
 120                                   repeat - the number of times to repeat the ticker label or -1 continuous
 121                                   facename overrides automatic selection of a fixed width font
 122                                    specify a specific font facename. e.g. 'Liberation Mono'
 123                                    Failure will drop back to automatic selection.
 124                                 Automatically selects the first fixed width font on your system.
 125                                 Ticker labels are constructed into a list prior to the ticker starting,
 126                                  so the heavy lifting is done on creation, not when running.
 127 
 128         SetTimer(milliseconds)
 129                                - The period of time before the text is cleared
 130                                  A value of < 1 turns off the clearing function
 131                                  Default -1
 132 
 133         TickerStop()   Stops the Ticker
 134 
 135         If the Flasher is running and a TimerLabel is cleared i.e. both timers were running
 136          at the same time, both timers are stopped, to avoid flashing a cleared text.
 137         A TimerLabel can be set to clear after say 10 seconds and be set to initially flash for 5 seconds for example
 138         A TimerLabel can be set to clear after say 10 seconds and be set to flash for 5 seconds before it clears
 139 
 140         Timers:
 141         clearer     - activates clearing    One Shot    Activates clearing
 142         clearing    - performs clear label  Continuous  Stops when label is clear
 143         flasher     - performs flashing     Continuous  Stops on Clearer activation
 144         cycling     - Cycles clearing text  One Shot    Repeats when clearing is finished
 145         tickering   - News ticker text      Continuous  Stops when told to or after the given number of repetitions
 146 
 147         CallLaters:
 148         flasher     - to stop after initial period
 149         flasher     - to start after wait period
 150 
 151         All timers Stop if you call Deactivate(), use IsActive() to test if ANY timer is running
 152 
 153     Events: EVT_CLEAR  event on ActiveText Clear
 154             Returns: Control Id, True, Control name
 155 
 156 Author:     J Healey
 157 Created:    20/01/2023
 158 Copyright:  J Healey - 2023
 159 Email:      <rolfofsaxony@gmx.com>
 160 
 161 Written and tested on Linux Python 3.8.10  wxPython 4.2.0 gtk3 (phoenix) wxWidgets 3.2.0
 162 Tested on Windows 11 courtesy of Ecco/ZigZag
 163 
 164 Usage examples:
 165 
 166 Example 1
 167 
 168 import wx
 169 from ActiveText import ActiveText
 170 
 171 class Frame(wx.Frame):
 172     def __init__(self, parent):
 173         wx.Frame.__init__(self, parent, -1, "Self Clearing Active Text Demo")
 174         panel = wx.Panel(self)
 175         box = wx.StaticBox(panel, wx.ID_ANY, "StaticBox", pos=(50, 50), size=(300, 50))
 176         text1 = ActiveText(box, -1, label="", style=0)
 177         text1.SetTimer(10000)
 178         text1.SetClearLabel("This text will disappear after 10 seconds")
 179 
 180         text2 = ActiveText(panel, -1, label="", pos=(50, 120), size=(-1,-1), style=0)
 181         text2.SetTimer(15000)
 182         text2.SetClearLabel("This text will flash for 4 seconds\n and disappear after 15 seconds")
 183         text2.SetFlash(period=100, timeout=4000)
 184 
 185         text3 = ActiveText(panel, -1, label="", pos=(50, 170), size=(-1,-1), style=0)
 186         text3.SetClearStyle('ltr', 25)
 187         text3.SetTimer(15000)
 188         text3.SetClearLabel("This text will flash for 5 seconds\n before it disappears after 15 seconds")
 189         text3.SetFlash(period=100, timeout=-5000)
 190 
 191         self.Show()
 192 
 193 app = wx.App()
 194 frame = Frame(None)
 195 app.MainLoop()
 196 
 197 Example 2
 198 Making use of the ActiveText Clear event
 199 
 200 import wx
 201 import ActiveText as act
 202 
 203 class Frame(wx.Frame):
 204     def __init__(self, parent):
 205         wx.Frame.__init__(self, parent, -1, "Self Clearing Static Text Demo")
 206         box = wx.StaticBox(self, wx.ID_ANY, "StaticBox", size=(300, 50))
 207         text1 = act.ActiveText(box, -1, label="", name="text1")
 208         text1.SetTimer(10000)
 209         text1.SetClearLabel("This text will disappear after 10 seconds")
 210 
 211         text2 = act.ActiveText(self, -1, label="", name="text2")
 212         text2.SetTimer(15000)
 213         text2.SetClearLabel("This text will flash for 4 seconds\n and disappear after 15 seconds")
 214         text2.SetFlash(period=100, timeout=4000)
 215 
 216         text3 = act.ActiveText(self, -1, label="", name="text3")
 217         text3.SetClearStyle('ltr', 100)
 218         text3.SetTimer(15000)
 219         text3.SetClearLabel("This text will flash for 5 seconds\n before disappearing Left (slowly) after 15 seconds")
 220         text3.SetFlash(period=100, timeout=-5000)
 221 
 222         text4 = act.ActiveText(self, -1, label="", name="text4")
 223         text4.SetClearStyle('rtl', 25)
 224         text4.SetTimer(15000)
 225         text4.SetClearLabel("This text will disappear after 15 seconds to the Right")
 226 
 227         text5 = act.ActiveText(self, label="", style=wx.ALIGN_LEFT, name="ticker")
 228         text5.SetTickerLabel("News Ticker running x 5", 100, 'rtl', maxchars=40, repeat=5)
 229 
 230         sizer = wx.BoxSizer(wx.VERTICAL)
 231         sizer.Add(box, 0, wx.ALL, 5)
 232         sizer.Add(text2, 0, wx.ALL, 5)
 233         sizer.Add(text3, 0, wx.ALL, 5)
 234         sizer.Add(text4, 0, wx.ALL, 5)
 235         sizer.Add(text5, 0, wx.ALL, 5)
 236         self.SetSizer(sizer)
 237         self.Bind(act.EVT_CLEAR, self.OnClear)
 238         self.Show()
 239 
 240     def OnClear(self, event):
 241         print(event.GetId(), event.value, event.name)
 242 
 243 app = wx.App()
 244 frame = Frame(None)
 245 app.MainLoop()
 246 '''
 247 
 248 actEVT_CLEAR = wx.NewEventType()
 249 EVT_CLEAR = wx.PyEventBinder(actEVT_CLEAR, 1)
 250 
 251 
 252 class ClearEvent(wx.PyCommandEvent):
 253     ''' Event sent from the :class:`ActiveText` when the control is cleared.
 254         EVT_CLEAR  The Control value has been cleared
 255     '''
 256     def __init__(self, eventType, eventId=-1, value=True, name=''):
 257         '''
 258         Default class constructor.
 259         :param `eventType`: the event type;
 260         :param `eventId`: the event identifier.
 261         '''
 262         wx.PyCommandEvent.__init__(self, eventType, eventId)
 263         self._eventType = eventType
 264         self.value = value
 265         self.name = name
 266 
 267 class ActiveText(wx.StaticText):
 268     def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition,
 269                  size=wx.DefaultSize, style=0, name=''):
 270         wx.StaticText.__init__(self, parent, id, label, pos, size, style, name)
 271         #super().__init__(parent, id, label, pos, size, style, name)
 272         self.clearer = wx.Timer(self)
 273         self.clearing = wx.Timer(self)
 274         self.flasher = wx.Timer(self)
 275         self.cycle = wx.Timer(self)
 276         self.ticker = wx.Timer(self)
 277         self.delay_active = None
 278         self.delay_start = None
 279         self.clear_style = None
 280         self.clear_style_period = 50
 281         self.clear_period = -1
 282         self.cycle_period = -1
 283         self.flashtext = label
 284         self.BaseFont = None
 285         self.Bind(wx.EVT_TIMER, self.OnClear, self.clearer)
 286         self.Bind(wx.EVT_TIMER, self.OnClearing, self.clearing)
 287         self.Bind(wx.EVT_TIMER, self.OnFlash, self.flasher)
 288         self.Bind(wx.EVT_TIMER, self.OnCycle, self.cycle)
 289         self.Bind(wx.EVT_TIMER, self.OnTick, self.ticker)
 290 
 291     def SetTimer(self, period=-1):
 292         '''
 293       - The period of time before the text is cleared
 294         A value of < 1 turns off the clearing function. Default -1
 295         '''
 296         self.clear_period = period
 297 
 298     def SetLabel(self, label=''):
 299         '''
 300         Used in place of the normal wx.StaticText.SetLabel(label)
 301         purely so that a standard SetLabel can automatically Deactivate the timers like other functions
 302         Without this function, you would have to remember to call Deactivate separately, before calling SetLabel
 303         in case any of the timers are running
 304         '''
 305         self.Deactivate()
 306         wx.StaticText.SetLabel(self, label)
 307         self.original_label = label
 308 
 309     def SetClearLabel(self, label='', period=-1):
 310         '''
 311         Sets the Label and starts the Clearing timer using the period value
 312         A period of < 0 turns off the Clearing function
 313         A period of None turns off clearing for just this occurrence
 314         '''
 315         if self.IsActive(): #turn off timers
 316             self.Deactivate()
 317         wx.StaticText.SetLabel(self,label)
 318         self.original_label = label
 319 
 320         if not period:
 321             return
 322         if period > 0: # Reset global period
 323             self.clear_period = period
 324         if self.clear_period > 0: # Use global time-out
 325             self.clearer.Start(self.clear_period, wx.TIMER_ONE_SHOT)
 326 
 327     def SetClearStyle(self, style=None, period=50):
 328         '''
 329         Defines how text will be cleared
 330         Style = None (default), text is cleared instantly
 331         Style = "ltr" (left to right) texted is cleared 1 character at a time from left to right
 332         Style = "rtl" (right to left) texted is cleared 1 character at a time from right to left
 333         Period in milliseconds (default = 50) between each character being removed.
 334         '''
 335         self.clear_style = style
 336         self.clear_style_period = period
 337 
 338     def OnClear(self, event):
 339         self.SetFlash(-1)
 340         self.clearing.Start(self.clear_style_period, wx.TIMER_CONTINUOUS)
 341 
 342     def OnClearing(self, event):
 343         lab = self.GetLabel()
 344         if self.clear_style: # Clear left to right or right to left
 345             if self.clear_style == 'ltr':
 346                 lab = lab[1:]
 347             else:
 348                 lab = " "+lab[:-1]
 349         else:
 350             lab = ''
 351 
 352         wx.StaticText.SetLabel(self,lab)
 353         if lab.strip(): # Label not empty or all spaces yet
 354             return
 355         self.clearing.Stop()
 356         # event clear
 357         self.ClearEvent()
 358         wx.StaticText.SetLabel(self,'')
 359         if self.cycle_period > 0: # Start cycling
 360             self.cycle.Start(self.cycle_period, wx.TIMER_ONE_SHOT)
 361 
 362     def ClearEvent(self):
 363         event = ClearEvent(actEVT_CLEAR, self.GetId(), True, self.GetName())
 364         event.SetEventObject(self)
 365         self.GetEventHandler().ProcessEvent(event)
 366 
 367     def SetFlash(self, period=-1, timeout=None): # negative timeout = delayed flash for clearing text
 368         '''
 369         Default period -1 ms Default timeout None
 370       - Starts/Stops the Flasher flashes every period, a period of 100 = 10 times a second
 371         period <= 0 deactivate the flasher
 372         timeout > 0 activate the flasher for timeout ms
 373         timeout < 0 delayed activation of flasher for timeout ms before a text Clearing
 374         '''
 375         if self.delay_active: # Cancel any existing CallLater
 376             self.delay_active.Stop()
 377             self.delay_active = None
 378         if period < 0 and self.flasher.IsRunning():
 379             self.flasher.Stop()
 380             wx.StaticText.SetLabel(self,self.flashtext)
 381             return
 382         if self.flasher.IsRunning():
 383             self.flasher.Stop()
 384         if period < 0:
 385             return
 386         if not timeout:                                                     # Simple Start flashing
 387             self.flasher.Start(period, wx.TIMER_CONTINUOUS)
 388         elif timeout > 0:                                                   # Start flashing with flash timeout
 389             self.flasher.Start(period, wx.TIMER_CONTINUOUS)
 390             self.delay_start = None
 391             self.delay_active = wx.CallLater(timeout, self.SetFlash)
 392         elif timeout < 0:                                                   # flashing before text clear
 393             if self.clearer.IsRunning():
 394                 interval = self.clearer.GetInterval()
 395                 fstart = interval + (timeout)                               # n seconds for before clear
 396                 if fstart > 0:
 397                     timeout =  abs(timeout) - interval
 398                     self.delay_start = wx.StopWatch()                       # Permits GetFlashCountdown
 399                     self.delay_start.Start(timeout)
 400                 else:
 401                     self.delay_start = None
 402                 self.delay_active = wx.CallLater(fstart,
 403                                                lambda per = period: self.StartReverseFlash(per)) # delayed flash
 404 
 405     def StartReverseFlash(self, period):
 406         self.delay_active = None
 407         self.delay_start = None
 408         self.flasher.Start(period, wx.TIMER_CONTINUOUS)
 409 
 410     def OnFlash(self, event):
 411         txt = self.GetLabel()
 412         try:
 413             if txt:
 414                 self.flashtext = txt
 415                 wx.StaticText.SetLabel(self,'')
 416             else:
 417                 wx.StaticText.SetLabel(self,self.flashtext)
 418         except Exception as e:
 419             self.flasher.Stop()
 420 
 421     def SetCycleLabel(self, label='', cycle=-1):
 422         '''
 423         Sets the Label and starts the Clearing timer using the clear_period value
 424         cycle - a period in ms to wait before re-presenting the text after it has been cleared
 425         '''
 426         self.Deactivate()
 427         wx.StaticText.SetLabel(self,label)
 428         self.original_label = label
 429         if cycle > 0:
 430             self.cycle_period = cycle
 431         if self.clear_period > 0: # Use global time-out
 432             self.clearer.Start(self.clear_period, wx.TIMER_ONE_SHOT)
 433 
 434     def OnCycle(self, event):
 435         self.SetCycleLabel(self.original_label, self.cycle_period)
 436 
 437     def CycleStop(self):
 438         self.cycle.Stop()
 439         self.cycle_period = -1
 440         wx.StaticText.SetLabel(self,'')
 441 
 442     def SetTickerLabel(self, label='', period=-1, direction="ltr", maxchars=-1, repeat=-1, facename=None):
 443         if self.IsActive(): #turn off timers
 444             self.Deactivate()
 445         self.tickerlabel = label.replace('\n', ' ')
 446         if period < 1 or not label:
 447             wx.StaticText.SetLabel(self,label)
 448             return
 449         wx.StaticText.SetLabel(self,'')
 450         ownfont = False
 451         self.BaseFont = self.GetFont()
 452         psize = self.BaseFont.GetPointSize()
 453         pstyle = self.BaseFont.GetStyle()
 454         pweight = self.BaseFont.GetWeight()
 455         if facename:
 456             newfont = self.BaseFont
 457             if newfont.SetFaceName(facename):
 458                 self.SetFont(newfont)
 459                 ownfont = True
 460             else:
 461                 ownfont = False
 462         if not facename or ownfont == False:
 463             fnts = wx.FontEnumerator()
 464             fnts.EnumerateFacenames()
 465             fnt_list = fnts.GetFacenames(fixedWidthOnly=True)   # get fixed width fonts
 466             if fnt_list:                                        # Grab the first one
 467                 newfont = wx.Font(fnt_list[0])
 468                 newfont.SetPointSize(psize)                     # adjust attrs
 469                 newfont.SetStyle(pstyle)
 470                 newfont.SetWeight(pweight)
 471                 self.SetFont(newfont)
 472         singw = self.GetTextExtent("e").GetWidth()
 473         w = self.GetSize().GetWidth()
 474         self.maxchars = int(w / singw)
 475         if maxchars > 0:
 476             self.maxchars = maxchars
 477         self.OnTickerLabels(direction)
 478         self.ticker_repeat = repeat
 479         self.ticker_index = 0
 480         self.ticker.Start(period, wx.TIMER_CONTINUOUS)
 481 
 482     def TickerStop(self):
 483         self.ticker.Stop()
 484         if self.BaseFont:
 485             self.SetFont(self.BaseFont)
 486         if self.flasher.IsRunning():
 487             self.flasher.Stop()
 488         wx.StaticText.SetLabel(self,'')
 489         self.ClearEvent()
 490 
 491     def OnTickerLabels(self, direction):
 492         self.tickerlabels = []
 493         if direction == 'ltr':
 494             label = " "*(self.maxchars) + self.tickerlabel
 495             limit = 0
 496             for i in range (1, len(label)+1):
 497                 lab = label[-i:]
 498                 if i > self.maxchars:
 499                     limit +=1
 500                     lab = lab[:-limit]
 501                 self.tickerlabels.append(lab)
 502         else:
 503             label = " "*(self.maxchars-1) + self.tickerlabel
 504             limit = self.maxchars
 505             for i in range(0,len(label)):
 506                 lab = label[i:limit]
 507                 limit += 1
 508                 self.tickerlabels.append(lab)
 509 
 510     def OnTick(self, event):
 511         if not self.tickerlabels or self.ticker_repeat == 0:
 512             self.TickerStop()
 513             return
 514         lab = self.tickerlabels[self.ticker_index]
 515         wx.StaticText.SetLabel(self,lab)
 516         self.ticker_index +=1
 517         if self.ticker_index > len(self.tickerlabels) - 1:
 518             self.ticker_index = 0
 519             self.ticker_repeat -= 1
 520 
 521     def IsTickering(self):
 522         '''
 523         Is the control Ticker returns True or False
 524         '''
 525         return self.ticker.IsRunning()
 526 
 527     def IsActive(self):
 528         '''
 529         Is the control Flashing, clearing, cycling or tickering returns True or False
 530         '''
 531         if self.IsClearing() or self.IsFlashing() or self.IsCycling() or self.IsTickering():
 532             return True
 533         else:
 534             return False
 535 
 536     def IsCycling(self):
 537         '''
 538         Is the control Cyling returns True or False
 539         '''
 540         if self.cycle_period > 0:
 541             return True
 542         else:
 543             return False
 544 
 545     def IsFlashing(self):
 546         '''
 547         Is the control Flashing returns True or False
 548         '''
 549         return self.flasher.IsRunning()
 550 
 551     def IsClearing(self):
 552         '''
 553         Is the control Clearing returns True or False
 554         '''
 555         if self.clearer.IsRunning() or self.clearing.IsRunning():
 556             return True
 557         else:
 558             return False
 559 
 560     def Deactivate(self):
 561         '''
 562         Stop all timers
 563         '''
 564         if self.delay_active: # Cancel any existing CallLater
 565             self.delay_active.Stop()
 566             self.delay_active = None
 567         self.flasher.Stop()
 568         self.delay_start = None
 569         self.clearing.Stop()
 570         self.clearer.Stop()
 571         self.TickerStop()
 572         self.CycleStop()
 573 
 574     def GetFlashPeriod(self):
 575         '''
 576         Returns period between flashes
 577         '''
 578         return self.flasher.GetInterval()
 579 
 580     def GetFlashDelay(self):
 581         '''
 582         Returns period set before flashing starts
 583         '''
 584         if self.delay_active:
 585             return self.delay_active.GetInterval()
 586         else:
 587             return None
 588 
 589     def GetFlashCountdown(self):
 590         '''
 591         Returns live calculated time before flashing starts or None
 592         '''
 593         if self.delay_active:
 594             if self.delay_start:
 595                 return abs(self.delay_start.Time())
 596         return None
 597 
 598 
 599     def GetCyclePeriod(self):
 600         '''
 601         Returns period between cycles
 602         '''
 603         return self.cycle_period
 604 
 605     def GetClearPeriod(self):
 606         '''
 607         Returns period before text is cleared
 608         '''
 609         return self.clear_period
 610 
 611 # ↓↓↓↓↓ Feel free to snip this last section off, it's only the demonstration section ↓↓↓↓↓ #
 612 
 613 class MainFrame(wx.Frame):
 614     def __init__(self):
 615         wx.Frame.__init__(self, None, -1, title='ActiveText Demonstration', size=(500, 580))
 616         panel = wx.Panel(self)
 617         self.text1 = ActiveText(panel, label="Active Text", pos=(10,10), size=(350,30), style=wx.ALIGN_LEFT, name='text1')
 618         self.button1 = wx.Button(panel, -1, "Self clearing Text", pos=(10,50))
 619         self.button2 = wx.Button(panel, -1, "Non clearing Text", pos=(10,90))
 620         self.button3 = wx.Button(panel, -1, "Normal Text", pos=(10,130))
 621         self.button4 = wx.Button(panel, -1, "Flashing On", pos=(10,170))
 622         self.button5 = wx.Button(panel, -1, "Flashing Off", pos=(10,210))
 623         self.button6 = wx.Button(panel, -1,  "Clearing Text 2", pos=(10,250))
 624         self.button7 = wx.Button(panel, -1,  "Flashing Clearing Text", pos=(10,290))
 625         self.button8 = wx.Button(panel, -1,  "Reverse Flashing Clearing Text", pos=(10,330))
 626         self.button9 = wx.Button(panel, -1,  "Clearing Left", pos=(10,370))
 627         self.button10 = wx.Button(panel, -1,  "Clearing Right", pos=(10,410))
 628         self.button11 = wx.Button(panel, -1,  "Cycling Text", pos=(10,450))
 629         self.button12 = wx.Button(panel, -1,  "Cycling Stop", pos=(130,450))
 630         self.button13 = wx.Button(panel, -1,  "Ticker", pos=(10,490))
 631         self.tdir = wx.Choice(panel, wx.ID_ANY, name="Tick dir", choices=['ltr','rtl'], pos=(130, 490))
 632         self.tdir.SetSelection(0)
 633         self.tdir.SetToolTip("Left to Right or Right to Left")
 634         self.period = wx.SpinCtrl(panel, -1, min=1000, max=20000, initial=5000, size=(110,-1), pos=(200,50))
 635         self.period.SetToolTip("Clear time period (ms)")
 636         self.flash_period = wx.SpinCtrl(panel, -1, min=50, max=1000, initial=100, size=(110,-1), pos=(200,170))
 637         self.flash_period.SetToolTip("Flash and Ticker period (ms)")
 638         self.flash_timeout = wx.SpinCtrl(panel, -1, min=-10000, max=10000, initial=-1, size=(110,-1), pos=(200,200))
 639         self.flash_timeout.SetToolTip("Flash timeout (ms) -1 = No Timeout" \
 640                                       "\n**** This is milliseconds ****\ne.g. Set to 3000 for 3 seconds")
 641         self.f_text = wx.TextCtrl(panel, wx.ID_ANY, value="Events and Timers\n", pos=(200,90), size=(280, 70),
 642                           style=wx.TE_MULTILINE| wx.TE_READONLY| wx.VSCROLL| wx.HSCROLL)
 643        #self.f_text = wx.StaticText(panel, label="", pos=(320,180))
 644         self.t_text = wx.StaticText(panel, label="Elapsed Time ", pos=(320,60))
 645         self.text9 = ActiveText(panel, label="Clear Left", pos=(320,370), name='clear1')
 646         self.text10 = ActiveText(panel, label="Clear Right", pos=(320,410), name='clear2')
 647         self.button1.Bind(wx.EVT_BUTTON, self.On_Button1)
 648         self.button2.Bind(wx.EVT_BUTTON, self.On_Button2)
 649         self.button3.Bind(wx.EVT_BUTTON, self.On_Button3)
 650         self.button4.Bind(wx.EVT_BUTTON, self.On_Button4)
 651         self.button5.Bind(wx.EVT_BUTTON, self.On_Button5)
 652         self.button6.Bind(wx.EVT_BUTTON, self.On_Button6)
 653         self.button7.Bind(wx.EVT_BUTTON, self.On_Button7)
 654         self.button8.Bind(wx.EVT_BUTTON, self.On_Button8)
 655         self.button9.Bind(wx.EVT_BUTTON, self.On_Button9)
 656         self.button10.Bind(wx.EVT_BUTTON, self.On_Button10)
 657         self.button11.Bind(wx.EVT_BUTTON, self.On_Button11)
 658         self.button11.Bind(wx.EVT_BUTTON, self.On_Button11)
 659         self.button12.Bind(wx.EVT_BUTTON, self.On_Button12)
 660         self.button13.Bind(wx.EVT_BUTTON, self.On_Button13)
 661         self.timer = wx.Timer(self)
 662         self.Bind(wx.EVT_TIMER, self.Flashing, self.timer)
 663         self.timer.Start(500)
 664         self.counter = 0
 665         self.font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
 666         self.font.SetPointSize(10)
 667         self.text1.SetFont(self.font)
 668         self.text9.SetFont(self.font)
 669         self.text10.SetFont(self.font)
 670         self.text1.Bind(EVT_CLEAR, self.OnClear)
 671         self.text9.Bind(EVT_CLEAR, self.OnClear)
 672         self.text10.Bind(EVT_CLEAR, self.OnClear)
 673         self.Show()
 674 
 675     # Setting Global period
 676     def On_Button1(self, event):
 677         self.text1.SetClearStyle(None)
 678         period = self.period.GetValue()
 679         self.text1.SetTimer(period)
 680         period = str(period/1000)
 681         self.text1.SetClearLabel("This text will disappear after "+ period +" seconds")
 682         self.counter = 0
 683         self.f_text.Clear()
 684 
 685     # No Clearing Period acts as StaticText
 686     def On_Button2(self, event):
 687         self.text1.SetTimer(0)
 688         self.text1.SetClearLabel("This text is permanent")
 689         self.counter = 0
 690         self.f_text.Clear()
 691 
 692     # Standard StaticText
 693     def On_Button3(self, event):
 694         self.text1.SetLabel("This is a standard StaticText Label")
 695         self.counter = 0
 696         self.f_text.Clear()
 697 
 698     # Start Flash
 699     def On_Button4(self, event):
 700         period = self.flash_period.GetValue()
 701         timeout=self.flash_timeout.GetValue()
 702         if timeout == -1:
 703             timeout = None
 704         self.text1.SetFlash(period, timeout)
 705         self.counter = 0
 706         self.f_text.Clear()
 707 
 708     # Stop Flash
 709     def On_Button5(self, event):
 710         self.text1.SetFlash(-1)
 711         self.counter = 0
 712         self.f_text.Clear()
 713 
 714     # Setting one-off period
 715     def On_Button6(self, event):
 716         self.text1.SetClearStyle(None)
 717         period = self.period.GetValue()
 718         self.text1.SetClearLabel("This text will disappear after 3 seconds", 3000)
 719         self.counter = 0
 720         self.f_text.Clear()
 721 
 722     # Clearing and Flashing
 723     def On_Button7(self, event):
 724         self.text1.SetClearStyle(None)
 725         period = self.period.GetValue()
 726         self.text1.SetTimer(period)
 727         period = str(period/1000)
 728         self.text1.SetClearLabel("This text will flash for 2 secs and disappear after "+ period +" seconds")
 729         self.text1.SetFlash(100, timeout=2000)
 730         self.counter = 0
 731         self.f_text.Clear()
 732 
 733     # Clearing and Reverse Flashing
 734     def On_Button8(self, event):
 735         self.text1.SetClearStyle('ltr', 25)
 736         period = self.period.GetValue()
 737         self.text1.SetTimer(period)
 738         period = str(period/1000)
 739         self.text1.SetClearLabel("This will disappear after "+ period +" seconds,\nFlashing for the last 3 secs, clearing  Left")
 740         self.text1.SetFlash(100, timeout=-3000)
 741         self.counter = 0
 742         self.f_text.Clear()
 743 
 744     # Clearing to the Left
 745     def On_Button9(self, event):
 746         self.text9.SetClearStyle('ltr', 50)
 747         period = self.period.GetValue()
 748         self.text9.SetTimer(period)
 749         self.text9.SetClearLabel("Clearing Left in "+str(period/1000))
 750         self.counter = 0
 751         self.f_text.Clear()
 752 
 753     # Clearing to the Right
 754     def On_Button10(self, event):
 755         self.text10.SetClearStyle('rtl', 50)
 756         period = self.period.GetValue()
 757         self.text10.SetTimer(period)
 758         self.text10.SetClearLabel("Clearing Right in "+str(period/1000))
 759         self.counter = 0
 760         self.f_text.Clear()
 761 
 762     # Clearing and Cycling
 763     def On_Button11(self, event):
 764         self.text1.SetClearStyle('ltr', 50)
 765         period = self.period.GetValue()
 766         self.text1.SetTimer(period)
 767         self.text1.SetCycleLabel("Clearing in "+str(period/1000)+" Returning in 6", cycle=6000)
 768         self.counter = 0
 769         self.f_text.Clear()
 770 
 771     # Cycle Stop
 772     def On_Button12(self, event):
 773         self.text1.CycleStop()
 774         self.counter = 0
 775         self.f_text.Clear()
 776 
 777     # Ticker
 778     def On_Button13(self, event):
 779         tdir = self.tdir.GetStringSelection()
 780         if tdir == 'ltr':
 781             self.text1.SetTickerLabel("News Ticker running continuous",
 782                                       self.flash_period.GetValue(), tdir, maxchars = 40)
 783         else:
 784             self.text1.SetTickerLabel("News Ticker running repeats x 5",
 785                                       self.flash_period.GetValue(), tdir, maxchars = 40, repeat=5)
 786         self.counter = 0
 787         self.f_text.Clear()
 788 
 789     # Demonstration information only
 790     def Flashing(self, event):
 791         txt = ''
 792         if self.text1.IsFlashing():
 793             txt = "Flashing"
 794         else:
 795             txt = ""
 796         if self.text1.IsClearing():
 797             txt += " Clearing"
 798         if self.text1.IsCycling():
 799             txt += " Cycling"
 800         if self.text1.IsTickering():
 801             txt += " Tickering"
 802         #self.f_text.SetLabel(txt)
 803         if txt:
 804             if self.counter % 1000 == 0:
 805                 end = " +\n"
 806             else:
 807                 end = "\n"
 808             self.f_text.write(txt + end)
 809         self.counter += 500
 810         self.t_text.SetLabel("Elapsed time " + str(self.counter/1000))
 811         if self.text1.GetFlashCountdown():
 812             self.f_text.write("Flash delay active: " + str(self.text1.GetFlashCountdown()) + "\n")
 813 
 814     def OnClear(self, event):
 815         self.f_text.write("Cleared "+ str(event.GetId()) + " " + str(event.name) + "\n")
 816 
 817 if __name__ == '__main__':
 818     app = wx.App()
 819     frame = MainFrame()
 820     app.MainLoop()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/

https://discuss.wxpython.org/t/activetext-rather-than-statictext/36370


Thanks to

J. Healey (aka RolfofSaxony), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

26/11/23 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

Active text rather than static text (Phoenix) (last edited 2024-08-16 17:57:42 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.