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 turs red and an error message is displayed on stausbar. 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 wxTimer class. Firstly, you need to create a wxTimer object.

self.timer = wxTimer(self, 1)

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

EVT_TIMER(self,  1, self.OnTimer)

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()

Code Sample

from wxPython.wx import *


class Isabelle(wxFrame):
    def __init__(self, parent, ID, title, size):
        wxFrame.__init__(self, parent, ID, title, (0,0), size,  style = wxDEFAULT_FRAME_STYLE )

        self.timer = wxTimer(self, 1)
        self.blick = 0

        self.statusbar = self.CreateStatusBar()
        self.statusbar.SetStatusText("Welcome to Isabelle")
        File = wxMenu()
        File.Append(30, "&Quit\tCtrl+Q", "Quit Isabelle")

        Help = wxMenu()
        Help.Append(31, "&About", "")


        menuBar = wxMenuBar()
        menuBar.Append(File, "&File")
        menuBar.Append(Help, "&Help")
        self.SetMenuBar(menuBar)

        tb = self.CreateToolBar( wxTB_HORIZONTAL | wxNO_BORDER | wxTB_FLAT | wxTB_TEXT)
        self.CommandLine = wxTextCtrl(tb, 21, size=(100, -1))
        self.LaunchCommand = wxButton(tb, 20, "Ok", size=wxSize(40, 20))

        tb.AddControl(self.CommandLine)
        tb.AddSeparator()
        tb.AddControl(self.LaunchCommand)
        tb.Realize()
        
        EVT_BUTTON(self, 20, self.OnLaunchCommandOk)
        EVT_TEXT_ENTER(self.CommandLine, 21, self.OnLaunchCommandOk)
        EVT_MENU(self, 31, self.OnAbout)
        EVT_MENU(self, 30,  self.OnExit)
        EVT_TIMER(self,  1, self.OnTimer)
     
        self.panel = wxPanel(self, -1, (0, 0), (500 , 300))
        self.panel.SetBackgroundColour("GRAY")
        self.sizer = wxBoxSizer(wxVERTICAL)
        self.sizer.Add(self.panel,1, wxEXPAND )
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
        self.Centre()


    def OnExit(self,e):
        dlg = wxMessageDialog(self, 'Are you sure to quit Isabelle?', 'Please Confirm', wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION)
        if dlg.ShowModal() == wxID_YES:
                self.Close(True) 
        

    def OnAbout(self, event):
        dlg = wxMessageDialog(self, "Isabelle\t\n"
                              "2004\t",
                              "About", wxOK | wxICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()


    def OnLaunchCommandOk(self, event):
        input = self.CommandLine.GetValue()
        if input == '/bye':
                self.OnExit(self)
        elif input == '/about':
                self.OnAbout(self)
        elif input == '/bell':
                wxBell()
        else:
                self.statusbar.SetBackgroundColour("RED")
                self.statusbar.SetStatusText("Unknown Command ")
                self.statusbar.Refresh()
                self.timer.Start(10)
        
        self.CommandLine.Clear()

    def OnTimer(self, event):
        self.blick = self.blick + 1
        if self.blick == 25:
                self.statusbar.SetBackgroundColour("#E0E2EB")
                self.statusbar.Refresh()

                self.timer.Stop()
                self.blick = 0


def main():
        app = wxPySimpleApp()
        isabelle = Isabelle(None, -1, "Isabelle", (-1, -1))
        isabelle.Show()
        app.MainLoop()


if __name__== '__main__':
        main()

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.

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

AlternaviteErrorMessages (last edited 2008-03-11 10:50:37 by localhost)

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