GUI - ponyatov/orth GitHub Wiki

We'll use wxPython to do simple GUI: editor and few windows let us view system state.

import wx           # wxPython
import wx.stc       # editor extension

Also, we need a separate thread for the interpreter

import threading

and the queue will be used by GUI to send commands to an interpreter

import Queue

wxPython application

Construct wxPython application

app = wx.App()

and start GUI event processing loop

app.MainLoop()

main window

Run,.. and nothing happens: to run GUI we need at least one active window (wxFrame):

main = wx.Frame(None,title=str(sys.argv))
main.Show()

wxFrame constructor parameters:

  • parent window = None
  • window title set to list of command parameters which was used to run system

menu

Not so impressive, we need some more.

Add menu let as save script text, and toggle debugging windows:

## main window menu
menubar = wx.MenuBar() ; main.SetMenuBar(menubar)

## file submenu
file = wx.Menu() ; menubar.Append(file,'&File')

## file/save
save = file.Append(wx.ID_SAVE,'&Save')

## file/quit
quit = file.Append(wx.ID_EXIT,'&Quit')

## debug submenu
debug = wx.Menu() ; menubar.Append(debug,'&Debug')

## debug/stack
stack = debug.Append(wx.ID_ANY,'&Stack\tF9',kind=wx.ITEM_CHECK)

## debug/words
words = debug.Append(wx.ID_ANY,'&Words\tF8',kind=wx.ITEM_CHECK)

## help submenu
help = wx.Menu() ; menubar.Append(help,'&Help')

## help/about
about = help.Append(wx.ID_ABOUT,'&About\tF1')

The menu was defined, but we need to add actions will be activated by menu item selection:

## file/quit
quit = file.Append(wx.ID_EXIT,'&Quit')
main.Bind(wx.EVT_MENU,lambda e:main.Close(),quit)

Here we bind menu event to the main window produced by quit menu item. We need only one method call to finish application, so we use lambda hint let us not to define callback function to process an event.

## help/about
about = help.Append(wx.ID_ABOUT,'&About\tF1')
## about event callback
def onAbout(event):
    AboutFile = open('README.md')
    # we need only first 8 lines
    info = AboutFile.readlines()[:8]
    # functional hint to convert list to string
    info = reduce(lambda a,b:a+b,info)
    AboutFile.close()
    # display (modal) message box
    wx.MessageBox(info,'About',wx.OK|wx.ICON_INFORMATION)
main.Bind(wx.EVT_MENU,onAbout,about)

next: editor