How to create a Tip of the day frame (Phoenix)

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


Introduction

"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.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

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


Here's a complete example of using

img_sample_one.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 MyApp
  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           #-------------------
  73 
  74           frameicon = wx.Icon("Icons/wxWidgets.ico")
  75           self.SetIcon(frameicon)
  76 
  77           #-------------------
  78 
  79           self.Bind(wx.EVT_CLOSE, self.on_close_window)
  80 
  81           #-------------------
  82 
  83           self.panel = wx.Panel(self, -1)
  84 
  85           self.btnTip = wx.Button(self.panel, -1,
  86                                   label="Show &tip setting dialog",
  87                                   pos=(20, 20),
  88                                   size=(230, 30))
  89           self.btnClose = wx.Button(self.panel, -1,
  90                                     label="&Quit",
  91                                     pos=(20, 100),
  92                                     size=(100, 30))
  93 
  94           self.Bind(wx.EVT_BUTTON, self.on_tip_btn, self.btnTip)
  95           self.Bind(wx.EVT_BUTTON, self.on_close_btn, self.btnClose)
  96 
  97           #-------------------
  98 
  99           # Simplified init method
 100           self.create_menu_bar()
 101 
 102           #-------------------
 103 
 104           self.CenterOnScreen()
 105 
 106      #------------------------
 107 
 108      def create_menu_bar(self):
 109           menuBar = wx.MenuBar()
 110 
 111           menuFile = wx.Menu(style=wx.MENU_TEAROFF)
 112           item = wx.MenuItem(menuFile, -1,
 113                              text="&Quit\tCtrl+Q",
 114                              helpString="Quit application.")
 115           menuFile.Append(item)
 116           self.Bind(wx.EVT_MENU, self.on_close_btn, item)
 117 
 118           menuOption = wx.Menu(style=wx.MENU_TEAROFF)
 119           item = wx.MenuItem(menuOption, -1,
 120                              text="&Tip\tCtrl+K",
 121                              helpString="Show or hide tip at start up.")
 122           menuOption.Append(item)
 123           self.Bind(wx.EVT_MENU, self.on_tip_btn, item)
 124 
 125           menuBar.Append(menuFile, title="&File")
 126           menuBar.Append(menuOption, title="&Option")
 127           self.SetMenuBar(menuBar)
 128 
 129      def show_tip(self):
 130           config = GetConfig()
 131 
 132           # Tip status (show or hide)
 133           showTip = config.ReadInt("ShowTips", 1)
 134           if showTip == 1 :
 135                print("Show tip = %s" % (True))
 136           elif showTip == 0 :
 137                print("Show tip = %s" % (False))
 138 
 139           index = config.ReadInt("TipIndex", 0)
 140           print("Index = %s" % (index))
 141 
 142 
 143           if showTip:
 144               tipProvider = CreateFileTipProvider("tips.txt", index)
 145               showTip = ShowTip(self, tipProvider)
 146               index = tipProvider.GetCurrentTip()
 147 
 148               config.WriteInt("ShowTips", showTip)
 149               config.WriteInt("TipIndex", index)
 150 
 151      def on_tip_btn(self, event):
 152           self.tip = MyTipOption(self)
 153 
 154      def on_close_btn(self, event):
 155           self.Close(True)
 156 
 157      def on_close_window(self, event):
 158           self.Destroy()
 159 
 160 #-----------------------------------------------------
 161 
 162 class MyApp(wx.App):
 163      def OnInit(self):
 164           self.SetAppName("Test_Tip")
 165 
 166           frame = MyFrame(None, -1, "Test show tip at start up")
 167           frame.Show(True)
 168           self.SetTopWindow(frame)
 169 
 170           if os.path.exists("tips.txt"):
 171               wx.CallAfter(frame.show_tip)
 172 
 173           return True
 174 
 175 #-----------------------------------------------------
 176 
 177 def GetConfig():
 178      config = wx.FileConfig(appName="Test_Tip",
 179                             localFilename=config_file)
 180      return config
 181 
 182 #-----------------------------------------------------
 183 
 184 if __name__ == "__main__":
 185      app = MyApp(False)
 186      app.MainLoop()


How change the widget text and internationalization support (i18n)

img_sample_two.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 MyApp
  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           #-------------------
  99 
 100           frameicon = wx.Icon("Icons/wxWidgets.ico")
 101           self.SetIcon(frameicon)
 102 
 103           #-------------------
 104 
 105           self.Bind(wx.EVT_CLOSE, self.on_close_window)
 106 
 107           #-------------------
 108 
 109           self.panel = wx.Panel(self, -1)
 110 
 111           self.btnTip = wx.Button(self.panel, -1,
 112                                   label=_("Show &tip setting dialog"),
 113                                   pos=(20, 20),
 114                                   size=(230, 30))
 115           self.btnClose = wx.Button(self.panel, -1,
 116                                     label=_("&Quit"),
 117                                     pos=(20, 100),
 118                                     size=(100, 30))
 119 
 120           self.Bind(wx.EVT_BUTTON, self.on_tip_btn, self.btnTip)
 121           self.Bind(wx.EVT_BUTTON, self.on_close_btn, self.btnClose)
 122 
 123           #-------------------
 124 
 125           # Simplified init method
 126           self.create_menu_bar()
 127 
 128           #-------------------
 129 
 130           self.CenterOnScreen()
 131 
 132      #------------------------
 133 
 134      def create_menu_bar(self):
 135           menuBar = wx.MenuBar()
 136 
 137           menuFile = wx.Menu(style=wx.MENU_TEAROFF)
 138           item = wx.MenuItem(menuFile, -1,
 139                              text=_("&Quit\tCtrl+Q"),
 140                              helpString=_("Quit application."))
 141           menuFile.Append(item)
 142           self.Bind(wx.EVT_MENU, self.on_close_btn, item)
 143 
 144           menuOption = wx.Menu(style=wx.MENU_TEAROFF)
 145           item = wx.MenuItem(menuOption, -1,
 146                              text=_("&Setting\tCtrl+K"),
 147                              helpString=_("Setting language and tip."))
 148           menuOption.Append(item)
 149           self.Bind(wx.EVT_MENU, self.on_tip_btn, item)
 150 
 151           menuBar.Append(menuFile, title=_("&File"))
 152           menuBar.Append(menuOption, title=_("&Option"))
 153           self.SetMenuBar(menuBar)
 154 
 155      def show_tip(self):
 156           config = GetConfig()
 157 
 158           # Tip status (show or hide)
 159           showTip = config.ReadInt("ShowTips", 1)
 160           if showTip == 1 :
 161                print("Show tip = %s" % (True))
 162           elif showTip == 0 :
 163                print("Show tip = %s" % (False))
 164 
 165           index = config.ReadInt("TipIndex", 0)
 166           print("Index = %s" % (index))
 167 
 168           if showTip:
 169               tipProvider = CreateFileTipProvider("tips.txt", index)
 170               showTip = ShowTip(self, tipProvider)
 171               index = tipProvider.GetCurrentTip()
 172 
 173               config.WriteInt("ShowTips", showTip)
 174               config.WriteInt("TipIndex", index)
 175 
 176      def on_tip_btn(self, event):
 177           self.tip = MyTipOption(self)
 178 
 179      def on_close_btn(self, event):
 180           self.Close(True)
 181 
 182      def on_close_window(self, event):
 183           self.Destroy()
 184 
 185 #-----------------------------------------------------
 186 
 187 class MyApp(wx.App):
 188      def OnInit(self):
 189           self.SetAppName("Test_Tip_i18n")
 190 
 191           # Retrieve the user configuration language
 192           config = GetConfig()
 193           language = config.Read("Language", "LANGUAGE_DEFAULT")
 194 
 195           # Setup the locale
 196           self.locale = wx.Locale(getattr(wx, language))
 197           path = os.path.abspath("./locale") + os.path.sep
 198           self.locale.AddCatalogLookupPathPrefix(path)
 199           self.locale.AddCatalog(self.GetAppName())
 200 
 201           frame = MyFrame(None, -1, _("Test show tip at start up - i18n"))
 202           frame.Show(True)
 203           self.SetTopWindow(frame)
 204 
 205           if os.path.exists("tips.txt"):
 206               wx.CallAfter(frame.show_tip)
 207 
 208           return True
 209 
 210 #-----------------------------------------------------
 211 
 212 def GetConfig():
 213      config = wx.FileConfig(appName="Test_Tip_i18n",
 214                             localFilename=config_file)
 215      return config
 216 
 217 #-----------------------------------------------------
 218 
 219 if __name__ == "__main__":
 220      app = MyApp(False)
 221      app.MainLoop()


Download source

source.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.

Link :

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

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

https://wiki.wxpython.org/WikiTipOfTheDay

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Robin Dunn, Cody Precord, the wxPython community...

Thanks also to all known contributors or anonymous that I forgot.


About this page

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

10/10/18 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to create a Tip of the day frame (Phoenix) (last edited 2020-12-13 13:56:00 by Ecco)

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