How to create an alternative error messages (Phoenix)

Keywords : Statusbar, Error message, Visual alert.


Introduction

When an error occurs in an application, an error dialog usually appears. This might get annoying. I have noticed a better solution in SAP system. When a user enters an invalid command, statusbar turns red and an error message is displayed on statusbar. The red colour catches the eye and the user can easily read the error message.


What Objects are Involved


Process Overview

There is a TextControl on the Toolbar. There you enter your commands. We have defined three commands. /bye, /about and /beep. If you mistype any of them, Statusbar turns red and displays an error. This is done with the wx.Timer class.

Firstly, you need to create a wx.Timer object :

  self.timer = wx.Timer(self, 1)

Then you bind this newly created object to a specific method. In our case OnTimer :

  self.Bind(wx.EVT_TIMER, self.OnTimer, id=1)

When the user enters an invalid command, timer object is being started with the following code :

  self.timer.Start(10)

Every 10 miliseconds OnTimer method is called. If variable self.blick equals 25, we stop the timer with :

  self.timer.Stop()


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

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


Code sample

img_sample_one.png

ICON file : wxwin.ico

   1 # sample_one.py
   2 
   3 import wx
   4 
   5 """
   6 
   7 Author : Jan Bodnar
   8 Website : zetcode.com
   9 
  10 """
  11 
  12 # class MyIsabelle
  13 # class MyApp
  14     
  15 #---------------------------------------------------------------------------
  16 
  17 class MyIsabelle(wx.Frame):
  18     def __init__(self, parent, id, title, size):
  19         wx.Frame.__init__(self, parent, id,
  20                           title, (0, 0), size,
  21                           style=wx.DEFAULT_FRAME_STYLE)
  22 
  23         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))        
  24         
  25         #------------
  26         
  27         self.timer = wx.Timer(self, 1)
  28 
  29         #------------
  30         
  31         self.blick = 0
  32 
  33         #------------
  34         
  35         self.statusbar = self.CreateStatusBar()
  36         self.statusbar.SetStatusText("Welcome to Isabelle")
  37 
  38         #------------
  39         
  40         File = wx.Menu()
  41         File.Append(30, "&Quit\tCtrl+Q", "Quit Isabelle")
  42 
  43         Help = wx.Menu()
  44         Help.Append(31, "&About", "About this program")
  45 
  46         menuBar = wx.MenuBar()
  47         menuBar.Append(File, "&File")
  48         menuBar.Append(Help, "&Help")
  49         self.SetMenuBar(menuBar)
  50 
  51         #------------
  52         
  53         tb = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER |
  54                                 wx.TB_FLAT | wx.TB_TEXT)
  55 
  56         self.CommandLine = wx.TextCtrl(tb, 21, size=(100, -1))
  57 
  58         self.LaunchCommand = wx.Button(tb, 20, "Ok", size=wx.Size(40, 20))
  59 
  60         tb.AddControl(self.CommandLine)
  61         tb.AddSeparator()
  62         tb.AddControl(self.LaunchCommand)
  63         tb.Realize()
  64 
  65         #------------
  66         
  67         self.Bind(wx.EVT_TEXT_ENTER, self.OnLaunchCommandOk, id=21)  
  68         self.Bind(wx.EVT_BUTTON, self.OnLaunchCommandOk, id=20)
  69         self.Bind(wx.EVT_MENU, self.OnAbout, id=31)
  70         self.Bind(wx.EVT_MENU, self.OnExit, id=30)
  71         self.Bind(wx.EVT_TIMER, self.OnTimer, id=1)
  72 
  73         #------------
  74         
  75         self.panel = wx.Panel(self, -1, (0, 0), (400 , 200))
  76         self.panel.SetBackgroundColour("#707070")
  77 
  78         #------------
  79         
  80         self.sizer = wx.BoxSizer(wx.VERTICAL)
  81         self.sizer.Add(self.panel, 1, wx.EXPAND )
  82 
  83         self.SetSizer(self.sizer)
  84         self.SetAutoLayout(1)
  85         self.sizer.Fit(self)
  86         self.Centre()
  87 
  88     #-----------------------------------------------------------------------
  89         
  90     def OnExit(self, event):
  91         """
  92         ...
  93         """
  94         
  95         dlg = wx.MessageDialog(self,
  96                                'Are you sure to quit Isabelle ?',
  97                                'Please Confirm',
  98                                wx.YES_NO |
  99                                wx.NO_DEFAULT |
 100                                wx.ICON_QUESTION)
 101 
 102         if dlg.ShowModal() == wx.ID_YES:
 103                 self.Close(True)         
 104 
 105 
 106     def OnAbout(self, event):
 107         """
 108         ...
 109         """
 110         
 111         dlg = wx.MessageDialog(self,
 112                                "Isabelle\t\n"
 113                                "2004\t",
 114                                "About",
 115                                wx.OK |
 116                                wx.ICON_INFORMATION)
 117         dlg.ShowModal()
 118         dlg.Destroy()
 119 
 120 
 121     def OnLaunchCommandOk(self, event):
 122         """
 123         ...
 124         """
 125         
 126         input = self.CommandLine.GetValue()
 127 
 128         if input == '/bye':
 129                 self.OnExit(self)
 130         elif input == '/about':
 131                 self.OnAbout(self)
 132         elif input == '/bell':
 133                 wx.Bell()
 134         else:
 135                 self.statusbar.SetBackgroundColour("RED")
 136                 self.statusbar.SetStatusText("Unknown Command ")
 137                 self.statusbar.Refresh()
 138                 self.timer.Start(10)
 139         
 140         self.CommandLine.Clear()
 141 
 142 
 143     def OnTimer(self, event):
 144         """
 145         ...
 146         """
 147         
 148         self.blick = self.blick + 1
 149 
 150         if self.blick == 25:
 151                 self.statusbar.SetBackgroundColour("#E0E2EB")
 152                 self.statusbar.Refresh()
 153 
 154                 self.timer.Stop()
 155                 self.blick = 0
 156 
 157 #---------------------------------------------------------------------------
 158 
 159 class MyApp(wx.App):
 160     def OnInit(self):
 161 
 162         #------------
 163 
 164         frame = MyIsabelle(None, -1,
 165                            "Isabelle",
 166                            (-1, -1))
 167         self.SetTopWindow(frame)
 168         frame.Show(True)
 169 
 170         return True
 171 
 172 #---------------------------------------------------------------------------
 173 
 174 def main():
 175     app = MyApp(redirect=False)
 176     app.MainLoop()
 177 
 178 #---------------------------------------------------------------------------
 179 
 180 if __name__ == "__main__" :
 181     main()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Jan Bodnar (sample_one.py coding), the wxPython community....


About this page

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

23/12/19 - Ecco (Updated page for wxPython Phoenix).


Comments

- It works perfectly at wxGTK (Debian Sarge, python 2.3.5, wxWidgets 2.6.1.1pre), but doesn't work at wxMSW (Windows XP SP2, python 2.3.5, wxWidgets 2.6.1.0). Only status message appears, no red blinks. -- Jurie Jurevich

- I didn't try this particular sample on windows. But the original code was written on windows, before I have completely abandoned python programming on windows. So it can be done, but it needs some tweaking. Writing portable code is very difficult, if possible. -- Jan Bodnar

How to create an alternative error messages (Phoenix) (last edited 2020-12-31 16:28:19 by Ecco)

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