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:
Some sort of persistence to remember whether tips are being shown and which tip will be shown next. For this recipe, I'm just referring to self.Persist, but I am not illustrating how self.Persist is created, saved, or restored.
You should also include:
- A settings panel that allows the user to turn the tips on/off, or a menu option for showing a tip of the day. Otherwise, if the user turns the tips off, they will not be able to turn them back on again!
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.
First example :
Here's a complete example of using :
1 import sys
2 import os
3 import wx
4
5 app_dir = os.path.split(os.path.abspath(sys.argv[0]))[0]
6 config_file = os.path.join(app_dir, "options.cfg")
7
8 #-----------------------------------------------------
9
10 class MyTipOption(wx.Dialog):
11 def __init__(self, parent):
12 wx.Dialog.__init__(self, parent, -1, title="Option tips")
13
14 self.Bind(wx.EVT_CLOSE, self.on_close_window)
15
16 #-------------------
17
18 config = GetConfig()
19 self.checkBox = wx.CheckBox(self, -1,
20 label="Show tips at start up",
21 pos=(20, 20))
22 self.checkBox.SetValue(config.ReadInt("ShowTips", 0))
23 content = self.checkBox.GetValue()
24 print "CheckBox = %s" % content
25
26 self.btnClose = wx.Button(self, -1,
27 label="&Close",
28 pos=(20, 120),
29 size=(100, 30))
30
31 self.checkBox.Bind(wx.EVT_CHECKBOX, self.on_check_box)
32 self.Bind(wx.EVT_BUTTON, self.on_close_btn, self.btnClose)
33
34 #-------------------
35
36 self.CentreOnParent(wx.BOTH)
37
38 #-------------------
39
40 self.btnClose = self.ShowModal()
41 self.Close()
42
43 #------------------------
44
45 def on_check_box(self, event):
46 config = GetConfig()
47 config.WriteInt("ShowTips", self.checkBox.GetValue())
48 content = self.checkBox.GetValue()
49 print "CheckBox = %s" % content
50
51 def on_close_btn(self, event):
52 self.Close(True)
53
54 def on_close_window(self, event):
55 self.Destroy()
56
57 #-----------------------------------------------------
58
59 class MyFrame(wx.Frame):
60 def __init__(self, parent, id, title):
61 wx.Frame.__init__(self, parent, -1, title)
62
63 self.Bind(wx.EVT_CLOSE, self.on_close_window)
64
65 #-------------------
66
67 self.panel = wx.Panel(self, -1)
68
69 self.btnTip = wx.Button(self.panel, -1,
70 label="Show &tip setting dialog",
71 pos=(20, 20),
72 size=(230, 30))
73 self.btnClose = wx.Button(self.panel, -1,
74 label="&Quit",
75 pos=(20, 100),
76 size=(100, 30))
77
78 self.Bind(wx.EVT_BUTTON, self.on_tip_btn, self.btnTip)
79 self.Bind(wx.EVT_BUTTON, self.on_close_btn, self.btnClose)
80
81 #-------------------
82
83 # Simplified init method
84 self.create_menu_bar()
85
86 #-------------------
87
88 self.CenterOnScreen()
89
90 #------------------------
91
92 def create_menu_bar(self):
93 menuBar = wx.MenuBar()
94
95 menuFile = wx.Menu(style=wx.MENU_TEAROFF)
96 item = wx.MenuItem(menuFile, -1,
97 text="&Quit\tCtrl+Q",
98 help="Quit application.")
99 menuFile.AppendItem(item)
100 self.Bind(wx.EVT_MENU, self.on_close_btn, item)
101
102 menuOption = wx.Menu(style=wx.MENU_TEAROFF)
103 item = wx.MenuItem(menuOption, -1,
104 text="&Tip\tCtrl+K",
105 help="Show or hide tip at start up.")
106 menuOption.AppendItem(item)
107 self.Bind(wx.EVT_MENU, self.on_tip_btn, item)
108
109 menuBar.Append(menuFile, title="&File")
110 menuBar.Append(menuOption, title="&Option")
111 self.SetMenuBar(menuBar)
112
113 def show_tip(self):
114 config = GetConfig()
115
116 # Tip status (show or hide)
117 showTip = config.ReadInt("ShowTips", 1)
118 if showTip == 1 :
119 print "Show tip = %s" % True
120 elif showTip == 0 :
121 print "Show tip = %s" % False
122
123 index = config.ReadInt("TipIndex", 0)
124 print "Index = %s" % index
125
126
127 if showTip:
128 tipProvider = wx.CreateFileTipProvider("tips.txt", index)
129 showTip = wx.ShowTip(self, tipProvider)
130 index = tipProvider.GetCurrentTip()
131
132 config.WriteInt("ShowTips", showTip)
133 config.WriteInt("TipIndex", index)
134
135 def on_tip_btn(self, event):
136 self.tip = MyTipOption(self)
137
138 def on_close_btn(self, event):
139 self.Close(True)
140
141 def on_close_window(self, event):
142 self.Destroy()
143
144 #-----------------------------------------------------
145
146 class App(wx.App):
147 def OnInit(self):
148 self.SetAppName("Test_Tip")
149
150 frame = MyFrame(None, -1, "Test show tip at start up")
151 frame.Show(True)
152 self.SetTopWindow(frame)
153
154 if os.path.exists("tips.txt"):
155 wx.CallAfter(frame.show_tip)
156
157 return True
158
159 #-----------------------------------------------------
160
161 def GetConfig():
162 config = wx.FileConfig(appName="Test_Tip",
163 localFilename=config_file)
164 return config
165
166 #-----------------------------------------------------
167
168 if __name__ == "__main__":
169 app = App(False)
170 app.MainLoop()
For the no-ascii characters, save the text file with an utf-8 encoding.
Second example :
A) - How change the widget's text.
B) - Support of internationalization (i18n) :
Show__Tip_At_Startup__i18n.zip
1 import sys
2 import os
3 import wx
4
5 app_dir = os.path.split(os.path.abspath(sys.argv[0]))[0]
6 config_file = os.path.join(app_dir, "options.cfg")
7
8 # Define a translation string
9 _ = wx.GetTranslation
10
11 #-----------------------------------------------------
12
13 class MyTipOption(wx.Dialog):
14 def __init__(self, parent):
15 wx.Dialog.__init__(self, parent, -1, title=_("Option"))
16
17 self.Bind(wx.EVT_CLOSE, self.on_close_window)
18
19 #-------------------
20
21 config = GetConfig()
22 self.checkBox = wx.CheckBox(self, -1,
23 label=_("Show tips at start up"),
24 pos=(20, 20))
25 self.checkBox.SetValue(config.ReadInt("ShowTips", 0))
26 content = self.checkBox.GetValue()
27 print "CheckBox = %s" % content
28
29 self.text = wx.StaticText(self, -1,
30 label=_("Language :"),
31 pos=(20, 68))
32
33 self.language = wx.Choice(self, -1,
34 choices=[_("English"),
35 _("French")],
36 pos=(100, 65),
37 size=(100, -1))
38
39 self.btnClose = wx.Button(self, -1,
40 label=_("&Close"),
41 pos=(20, 120),
42 size=(100, 30))
43
44 self.checkBox.Bind(wx.EVT_CHECKBOX, self.on_check_box)
45 self.Bind(wx.EVT_CHOICE, self.on_language)
46 self.Bind(wx.EVT_BUTTON, self.on_close_btn, self.btnClose)
47
48 #-------------------
49
50 self.CentreOnParent(wx.BOTH)
51
52 #-------------------
53
54 self.btnClose = self.ShowModal()
55 self.Close()
56
57 #------------------------
58
59 def on_language(self, event):
60 choice = self.language.GetSelection()
61
62 config = GetConfig()
63 if choice == 0:
64 val = "LANGUAGE_ENGLISH"
65 else:
66 val = "LANGUAGE_FRENCH"
67 config.Write("Language", val)
68
69 def on_check_box(self, event):
70 config = GetConfig()
71 config.WriteInt("ShowTips", self.checkBox.GetValue())
72 content = self.checkBox.GetValue()
73 print "CheckBox = %s" % content
74
75 def on_close_btn(self, event):
76 self.Close(True)
77
78 def on_close_window(self, event):
79 self.Destroy()
80
81 #-----------------------------------------------------
82
83 class MyFrame(wx.Frame):
84 def __init__(self, parent, id, title):
85 wx.Frame.__init__(self, parent, -1, title)
86
87 self.Bind(wx.EVT_CLOSE, self.on_close_window)
88
89 #-------------------
90
91 self.panel = wx.Panel(self, -1)
92
93 self.btnTip = wx.Button(self.panel, -1,
94 label=_("Show &tip setting dialog"),
95 pos=(20, 20),
96 size=(230, 30))
97 self.btnClose = wx.Button(self.panel, -1,
98 label=_("&Quit"),
99 pos=(20, 100),
100 size=(100, 30))
101
102 self.Bind(wx.EVT_BUTTON, self.on_tip_btn, self.btnTip)
103 self.Bind(wx.EVT_BUTTON, self.on_close_btn, self.btnClose)
104
105 #-------------------
106
107 # Simplified init method
108 self.create_menu_bar()
109
110 #-------------------
111
112 self.CenterOnScreen()
113
114 #------------------------
115
116 def create_menu_bar(self):
117 menuBar = wx.MenuBar()
118
119 menuFile = wx.Menu(style=wx.MENU_TEAROFF)
120 item = wx.MenuItem(menuFile, -1,
121 text=_("&Quit\tCtrl+Q"),
122 help=_("Quit application."))
123 menuFile.AppendItem(item)
124 self.Bind(wx.EVT_MENU, self.on_close_btn, item)
125
126 menuOption = wx.Menu(style=wx.MENU_TEAROFF)
127 item = wx.MenuItem(menuOption, -1,
128 text=_("&Setting\tCtrl+K"),
129 help=_("Setting language and tip."))
130 menuOption.AppendItem(item)
131 self.Bind(wx.EVT_MENU, self.on_tip_btn, item)
132
133 menuBar.Append(menuFile, title=_("&File"))
134 menuBar.Append(menuOption, title=_("&Option"))
135 self.SetMenuBar(menuBar)
136
137 def show_tip(self):
138 config = GetConfig()
139
140 # Tip status (show or hide)
141 showTip = config.ReadInt("ShowTips", 1)
142 if showTip == 1 :
143 print "Show tip = %s" % True
144 elif showTip == 0 :
145 print "Show tip = %s" % False
146
147 index = config.ReadInt("TipIndex", 0)
148 print "Index = %s" % index
149
150 if showTip:
151 tipProvider = wx.CreateFileTipProvider("tips.txt", index)
152 showTip = wx.ShowTip(self, tipProvider)
153 index = tipProvider.GetCurrentTip()
154
155 config.WriteInt("ShowTips", showTip)
156 config.WriteInt("TipIndex", index)
157
158 def on_tip_btn(self, event):
159 self.tip = MyTipOption(self)
160
161 def on_close_btn(self, event):
162 self.Close(True)
163
164 def on_close_window(self, event):
165 self.Destroy()
166
167 #-----------------------------------------------------
168
169 class App(wx.App):
170 def OnInit(self):
171 self.SetAppName("Test_Tip_i18n")
172
173 # Retrieve the user configuration language
174 config = GetConfig()
175 language = config.Read("Language", "LANGUAGE_DEFAULT")
176
177 # Setup the locale
178 self.locale = wx.Locale(getattr(wx, language))
179 path = os.path.abspath("./locale") + os.path.sep
180 self.locale.AddCatalogLookupPathPrefix(path)
181 self.locale.AddCatalog(self.GetAppName())
182
183 frame = MyFrame(None, -1, _("Test show tip at start up - i18n"))
184 frame.Show(True)
185 self.SetTopWindow(frame)
186
187 if os.path.exists("tips.txt"):
188 wx.CallAfter(frame.show_tip)
189
190 return True
191
192 #-----------------------------------------------------
193
194 def GetConfig():
195 config = wx.FileConfig(appName="Test_Tip_i18n",
196 localFilename=config_file)
197 return config
198
199 #-----------------------------------------------------
200
201 if __name__ == "__main__":
202 app = App(False)
203 app.MainLoop()
For the non-ascii characters, save the text file with an utf-8 encoding.
Download attachments for the test (see below).
