Tip of the Day Recipe

"Tip of the day" is easily implemented with the wxCreateFileTipProvider function and a text file full of tips.

Your application will need:

You should also include:


The Recipe

First, create a text file (tips.txt) with helpful tips in it. Lines that begin with a "#" will be treated as comments:

# Dental hygene tips:
Brush your teeth three times a day.
Floss every day.
An apple a day keeps the doctor away.

In your frame's intialization, create a tip provider:

   1 self.TipOfTheDay = wx.CreateFileTipProvider("tips.txt", self.Persist.TipOfTheDayIndex)
   2 if self.Persist.ShowTotD:
   3     wx.CallAfter(self.ShowTipOfTheDay)

Note that I'm doing a wx.CallAfter so that the initialization process doesn't grind to a halt while waiting for the user to dismiss the tip.

The ShowTipOfTheDay function is:

   1 def ShowTipOfTheDay(self):
   2     self.Persist.ShowTotD = wx.ShowTip(self, self.TipOfTheDay, self.Persist.ShowTotD)

Finally, when my application is shutting down, I recapture the current tip number so that the tips will pick up from where they left off on the next run.

   1 self.Persist.TipOfTheDayIndex = self.TipOfTheDay.CurrentTip

That CurrentTip member doesn't appear to be documented in the help files, but it does seem to do the trick.


wxPython "Phoenix" - Ready to use :


Keywords : Tip of the Day, CreateFileTipProvider, ShowTip, TipProvider.


Tested py3.x, wx4.x and Win10.


Here's a complete example of using

native-new.png

   1 # sample_one.py
   2 
   3 import sys
   4 import os
   5 import wx
   6 from wx.adv import CreateFileTipProvider
   7 from wx.adv import ShowTip
   8 from wx.adv import TipProvider
   9 
  10 # class MyTipOption
  11 # class MyFrame
  12 # class App
  13 
  14 app_dir = os.path.split(os.path.abspath(sys.argv[0]))[0]
  15 config_file = os.path.join(app_dir, "options.cfg")
  16 
  17 #-----------------------------------------------------
  18 
  19 class MyTipOption(wx.Dialog):
  20      def __init__(self, parent):
  21           wx.Dialog.__init__(self, parent, -1, title="Option tips")
  22 
  23           self.Bind(wx.EVT_CLOSE, self.on_close_window)
  24 
  25           #-------------------
  26 
  27           config = GetConfig()
  28           self.checkBox = wx.CheckBox(self, -1,
  29                                       label="Show tips at start up",
  30                                       pos=(20, 20))
  31           self.checkBox.SetValue(config.ReadInt("ShowTips", 0))
  32           content = self.checkBox.GetValue()
  33           print("CheckBox = %s" % (content))
  34 
  35           self.btnClose = wx.Button(self, -1,
  36                                     label="&Close",
  37                                     pos=(20, 120),
  38                                     size=(100, 30))
  39 
  40           self.checkBox.Bind(wx.EVT_CHECKBOX, self.on_check_box)
  41           self.Bind(wx.EVT_BUTTON, self.on_close_btn, self.btnClose)
  42 
  43           #-------------------
  44 
  45           self.CentreOnParent(wx.BOTH)
  46 
  47           #-------------------
  48 
  49           self.btnClose = self.ShowModal()
  50           self.Close()
  51 
  52      #------------------------
  53 
  54      def on_check_box(self, event):
  55           config = GetConfig()
  56           config.WriteInt("ShowTips", self.checkBox.GetValue())
  57           content = self.checkBox.GetValue()
  58           print("CheckBox = %s" % (content))
  59 
  60      def on_close_btn(self, event):
  61           self.Close(True)
  62 
  63      def on_close_window(self, event):
  64           self.Destroy()
  65 
  66 #-----------------------------------------------------
  67 
  68 class MyFrame(wx.Frame):
  69      def __init__(self, parent, id, title):
  70           wx.Frame.__init__(self, parent, -1, title)
  71 
  72           self.Bind(wx.EVT_CLOSE, self.on_close_window)
  73 
  74           #-------------------
  75 
  76           self.panel = wx.Panel(self, -1)
  77 
  78           self.btnTip = wx.Button(self.panel, -1,
  79                                   label="Show &tip setting dialog",
  80                                   pos=(20, 20),
  81                                   size=(230, 30))
  82           self.btnClose = wx.Button(self.panel, -1,
  83                                     label="&Quit",
  84                                     pos=(20, 100),
  85                                     size=(100, 30))
  86 
  87           self.Bind(wx.EVT_BUTTON, self.on_tip_btn, self.btnTip)
  88           self.Bind(wx.EVT_BUTTON, self.on_close_btn, self.btnClose)
  89 
  90           #-------------------
  91 
  92           # Simplified init method
  93           self.create_menu_bar()
  94 
  95           #-------------------
  96 
  97           self.CenterOnScreen()
  98 
  99      #------------------------
 100 
 101      def create_menu_bar(self):
 102           menuBar = wx.MenuBar()
 103 
 104           menuFile = wx.Menu(style=wx.MENU_TEAROFF)
 105           item = wx.MenuItem(menuFile, -1,
 106                              text="&Quit\tCtrl+Q",
 107                              helpString="Quit application.")
 108           menuFile.Append(item)
 109           self.Bind(wx.EVT_MENU, self.on_close_btn, item)
 110 
 111           menuOption = wx.Menu(style=wx.MENU_TEAROFF)
 112           item = wx.MenuItem(menuOption, -1,
 113                              text="&Tip\tCtrl+K",
 114                              helpString="Show or hide tip at start up.")
 115           menuOption.Append(item)
 116           self.Bind(wx.EVT_MENU, self.on_tip_btn, item)
 117 
 118           menuBar.Append(menuFile, title="&File")
 119           menuBar.Append(menuOption, title="&Option")
 120           self.SetMenuBar(menuBar)
 121 
 122      def show_tip(self):
 123           config = GetConfig()
 124 
 125           # Tip status (show or hide)
 126           showTip = config.ReadInt("ShowTips", 1)
 127           if showTip == 1 :
 128                print("Show tip = %s" % (True))
 129           elif showTip == 0 :
 130                print("Show tip = %s" % (False))
 131 
 132           index = config.ReadInt("TipIndex", 0)
 133           print("Index = %s" % (index))
 134 
 135 
 136           if showTip:
 137               tipProvider = CreateFileTipProvider("tips.txt", index)
 138               showTip = ShowTip(self, tipProvider)
 139               index = tipProvider.GetCurrentTip()
 140 
 141               config.WriteInt("ShowTips", showTip)
 142               config.WriteInt("TipIndex", index)
 143 
 144      def on_tip_btn(self, event):
 145           self.tip = MyTipOption(self)
 146 
 147      def on_close_btn(self, event):
 148           self.Close(True)
 149 
 150      def on_close_window(self, event):
 151           self.Destroy()
 152 
 153 #-----------------------------------------------------
 154 
 155 class App(wx.App):
 156      def OnInit(self):
 157           self.SetAppName("Test_Tip")
 158 
 159           frame = MyFrame(None, -1, "Test show tip at start up")
 160           frame.Show(True)
 161           self.SetTopWindow(frame)
 162 
 163           if os.path.exists("tips.txt"):
 164               wx.CallAfter(frame.show_tip)
 165 
 166           return True
 167 
 168 #-----------------------------------------------------
 169 
 170 def GetConfig():
 171      config = wx.FileConfig(appName="Test_Tip",
 172                             localFilename=config_file)
 173      return config
 174 
 175 #-----------------------------------------------------
 176 
 177 if __name__ == "__main__":
 178      app = App(False)
 179      app.MainLoop()


How change the widget's text and support of internationalization (i18n)

native-new.png i18n-new.png

   1 # sample_two.py
   2 
   3 import sys
   4 import os
   5 import wx
   6 from wx.adv import CreateFileTipProvider
   7 from wx.adv import ShowTip
   8 from wx.adv import TipProvider
   9 
  10 # class MyTipOption
  11 # class MyFrame
  12 # class App
  13 
  14 app_dir = os.path.split(os.path.abspath(sys.argv[0]))[0]
  15 config_file = os.path.join(app_dir, "options.cfg")
  16 
  17 # Define a translation string
  18 _ = wx.GetTranslation
  19 
  20 #-----------------------------------------------------
  21 
  22 class MyTipOption(wx.Dialog):
  23      def __init__(self, parent):
  24           wx.Dialog.__init__(self, parent, -1, title=_("Option"))
  25 
  26           self.Bind(wx.EVT_CLOSE, self.on_close_window)
  27 
  28           #-------------------
  29 
  30           config = GetConfig()
  31           self.checkBox = wx.CheckBox(self, -1,
  32                                       label=_("Show tips at start up"),
  33                                       pos=(20, 20))
  34           self.checkBox.SetValue(config.ReadInt("ShowTips", 0))
  35           content = self.checkBox.GetValue()
  36           print("CheckBox = %s" % (content))
  37 
  38           self.text = wx.StaticText(self, -1,
  39                                     label=_("Language :"),
  40                                     pos=(20, 68))
  41 
  42           self.language = wx.Choice(self, -1,
  43                                     choices=[_("English"),
  44                                              _("French")],
  45                                     pos=(100, 65),
  46                                     size=(100, -1))
  47 
  48           self.btnClose = wx.Button(self, -1,
  49                                     label=_("&Close"),
  50                                     pos=(20, 120),
  51                                     size=(100, 30))
  52 
  53           self.checkBox.Bind(wx.EVT_CHECKBOX, self.on_check_box)
  54           self.Bind(wx.EVT_CHOICE, self.on_language)
  55           self.Bind(wx.EVT_BUTTON, self.on_close_btn, self.btnClose)
  56 
  57           #-------------------
  58 
  59           self.CentreOnParent(wx.BOTH)
  60 
  61           #-------------------
  62 
  63           self.btnClose = self.ShowModal()
  64           self.Close()
  65 
  66      #------------------------
  67 
  68      def on_language(self, event):
  69           choice = self.language.GetSelection()
  70           print("Selection = %s" % (choice))
  71 
  72           config = GetConfig()
  73           if choice == 0:
  74                val = "LANGUAGE_ENGLISH"
  75           else:
  76                val = "LANGUAGE_FRENCH"
  77           print("Language = %s" % (val))
  78           config.Write("Language", val)
  79 
  80      def on_check_box(self, event):
  81           config = GetConfig()
  82           config.WriteInt("ShowTips", self.checkBox.GetValue())
  83           content = self.checkBox.GetValue()
  84           print("CheckBox = %s" % (content))
  85 
  86      def on_close_btn(self, event):
  87           self.Close(True)
  88 
  89      def on_close_window(self, event):
  90           self.Destroy()
  91 
  92 #-----------------------------------------------------
  93 
  94 class MyFrame(wx.Frame):
  95      def __init__(self, parent, id, title):
  96           wx.Frame.__init__(self, parent, -1, title)
  97 
  98           self.Bind(wx.EVT_CLOSE, self.on_close_window)
  99 
 100           #-------------------
 101 
 102           self.panel = wx.Panel(self, -1)
 103 
 104           self.btnTip = wx.Button(self.panel, -1,
 105                                   label=_("Show &tip setting dialog"),
 106                                   pos=(20, 20),
 107                                   size=(230, 30))
 108           self.btnClose = wx.Button(self.panel, -1,
 109                                     label=_("&Quit"),
 110                                     pos=(20, 100),
 111                                     size=(100, 30))
 112 
 113           self.Bind(wx.EVT_BUTTON, self.on_tip_btn, self.btnTip)
 114           self.Bind(wx.EVT_BUTTON, self.on_close_btn, self.btnClose)
 115 
 116           #-------------------
 117 
 118           # Simplified init method
 119           self.create_menu_bar()
 120 
 121           #-------------------
 122 
 123           self.CenterOnScreen()
 124 
 125      #------------------------
 126 
 127      def create_menu_bar(self):
 128           menuBar = wx.MenuBar()
 129 
 130           menuFile = wx.Menu(style=wx.MENU_TEAROFF)
 131           item = wx.MenuItem(menuFile, -1,
 132                              text=_("&Quit\tCtrl+Q"),
 133                              helpString=_("Quit application."))
 134           menuFile.Append(item)
 135           self.Bind(wx.EVT_MENU, self.on_close_btn, item)
 136 
 137           menuOption = wx.Menu(style=wx.MENU_TEAROFF)
 138           item = wx.MenuItem(menuOption, -1,
 139                              text=_("&Setting\tCtrl+K"),
 140                              helpString=_("Setting language and tip."))
 141           menuOption.Append(item)
 142           self.Bind(wx.EVT_MENU, self.on_tip_btn, item)
 143 
 144           menuBar.Append(menuFile, title=_("&File"))
 145           menuBar.Append(menuOption, title=_("&Option"))
 146           self.SetMenuBar(menuBar)
 147 
 148      def show_tip(self):
 149           config = GetConfig()
 150 
 151           # Tip status (show or hide)
 152           showTip = config.ReadInt("ShowTips", 1)
 153           if showTip == 1 :
 154                print("Show tip = %s" % (True))
 155           elif showTip == 0 :
 156                print("Show tip = %s" % (False))
 157 
 158           index = config.ReadInt("TipIndex", 0)
 159           print("Index = %s" % (index))
 160 
 161           if showTip:
 162               tipProvider = CreateFileTipProvider("tips.txt", index)
 163               showTip = ShowTip(self, tipProvider)
 164               index = tipProvider.GetCurrentTip()
 165 
 166               config.WriteInt("ShowTips", showTip)
 167               config.WriteInt("TipIndex", index)
 168 
 169      def on_tip_btn(self, event):
 170           self.tip = MyTipOption(self)
 171 
 172      def on_close_btn(self, event):
 173           self.Close(True)
 174 
 175      def on_close_window(self, event):
 176           self.Destroy()
 177 
 178 #-----------------------------------------------------
 179 
 180 class App(wx.App):
 181      def OnInit(self):
 182           self.SetAppName("Test_Tip_i18n")
 183 
 184           # Retrieve the user configuration language
 185           config = GetConfig()
 186           language = config.Read("Language", "LANGUAGE_DEFAULT")
 187 
 188           # Setup the locale
 189           self.locale = wx.Locale(getattr(wx, language))
 190           path = os.path.abspath("./locale") + os.path.sep
 191           self.locale.AddCatalogLookupPathPrefix(path)
 192           self.locale.AddCatalog(self.GetAppName())
 193 
 194           frame = MyFrame(None, -1, _("Test show tip at start up - i18n"))
 195           frame.Show(True)
 196           self.SetTopWindow(frame)
 197 
 198           if os.path.exists("tips.txt"):
 199               wx.CallAfter(frame.show_tip)
 200 
 201           return True
 202 
 203 #-----------------------------------------------------
 204 
 205 def GetConfig():
 206      config = wx.FileConfig(appName="Test_Tip_i18n",
 207                             localFilename=config_file)
 208      return config
 209 
 210 #-----------------------------------------------------
 211 
 212 if __name__ == "__main__":
 213      app = App(False)
 214      app.MainLoop()


Download sources

Sources.zip


Additional Information


For the non-ascii characters, save the text file with an utf-8 encoding.

For the translation or editing, you can use Poedit application.

It exist some version for Windows, Mac and Linux.

Links :

https://en.wikipedia.org/wiki/Poedit

https://fr.wikipedia.org/wiki/Poedit

TipOfTheDay (last edited 2018-09-20 02:13:43 by Jean-Charles)

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