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

Create an alternative error messages (Phoenix) (last edited 2020-10-25 15:56:14 by Ecco)

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