Re: Improved Layout by Code module for Python

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Fri, 17 Jun 2011 06:32:45 -0400

Yes, I should have mentioned that. The section name in the ini file corresponds to the title of the dialog.

BTW, here is a revised version of your test script using the new

import wx, lbc

def evt_handler(d, e, n):
# This code block prints the type of event and the name of the widget reacting to it
 if lbc.IsInitEvent(e): print 'Init'
 elif lbc.IsFocusEvent(e): print 'Focus'
 elif lbc.IsClickEvent(e): print 'Click'
 elif lbc.IsCloseEvent(e): print 'Close'
 else: print 'Other event'
 print n

 if lbc.IsCloseEvent(e): return d.Destroy()
elif lbc.IsClickEvent(e): lbc.DialogShow(title="popup", message=n + " was clicked")

dlg.Complete(buttons=['OK', 'Cancel'], handler=evt_handler)


On 6/17/2011 6:18 AM, Alex Hall wrote:
Thanks for the update, I am going to try it to see if I can get things
working better. My only question for now is about the ini file. In the
sample, you have a [Fruit Basket] section. How did you decide on that
section name? Does it have to match the title of the dialog with which
it is associated?

On 6/16/11, Jamal Mazrui<empower@xxxxxxxxx>  wrote:
   From the archive at

Layout by Code for Python
Version 1.3
June 16, 2011
Copyright 2009 - 2011 by Jamal Mazrui
GNU Lesser General Public License (LGPL)

I have updated a Python module called Layout by Code (lbc), which
simplifies design of dialogs with the WxWidgets GUI library.  It is
another implementation of the Layout by Code approach that I originally
developed for the AutoIt language, available at

I also developed a version for .NET languages, such as C# and Visual
Basic, which is distributed as part of HomerApp, the Homer Application
Framework, available at

Although the Python version is not as flexible, it is still intended to
support most dialogs one might need (based on years of experience with
various applications and languages).  It should work with Python
versions 2.4 through 2.7.  It depends on the following 3rd-party Python



Python for Windows extensions

odict -- an ordered dictionary

The py2exe module is only needed if one wants to create a Windows
executable like lbc_fruit.exe.  The batch file run_setup_lbc_fruit.bat
does this, using to specify how the executable is built.

After instantiating an lbc dialog object, any of the following controls
may be added with a line of code:  Button, CheckBox, ListBox,
RadioButton, RichEdit, StaticText, or TextCtrl.  The control is added to
a horizontal band of controls, with layout automatically managed by
wxSizer containers.  Adding a new band is analagous to pressing carriage
return at the end of a line.

The lbc Complete method adds a band of buttons at the bottom of the
dialog before invoking it.  The method returns the ID of the button that
ended the dialog.  At that point, the dialog object has an ordered
dictionary attribute called Controls, containing the names of controls
that were added to the dialog before it was invoked.  A control name is
its class and label, if any, seperated by an underscore, e.g.,
Button_OK.  The dialog also has a dictionary attribute called Results,
containing the control names and values when the dialog ended.

By default, a status bar is added as the last control of a dialog.  It
may be used for help messages to the user.  For example, each control
can trigger a help message when it receives focus.  These messages may
be defined in a .ini file that accompanies the main script file (with
the same name except for the extension).  The fruit basket sample script
illustrates this feature in the file lbc_fruit.ini.  A screen reader
user can press a hotkey to read the status bar.

Over ten convenience dialogs have been defined using lbc, illustrated
with the program  Also, a fruit basket program is in

Below is a summary of lbc functions.  For convenient reference, the code
in and then follows afterward.


lbc convenience dialogs

DialogBrowseForFolder(title='', value='') -- Select a folder

DialogChoose(title='Choose', message = '', names=[]) -- Choose a button

DialogConfirm(title='Confirm', message='', value='Y') -- Choose from a
Yes/No/Cancel message box

DialogInput(title='Input', label='', value='', ) -- Input with a
single-line edit box

DialogMemo(title='Memo', label='', value='', readonly=False) -- Input or
read text with a multiple-line edit box

DialogMultiInput(title='MultiInput', labels=[], values=[], options=[])
-- Input with multiple edit boxes

DialogMultiPick(title='Multi Pick', message='', names=[], values=[],
sort=False, index=0) -- Pick from a multiple-selection listbox

DialogOpenFile(title='Open', value='', wildcard='All files (*.*)|*.*')
-- Specify a file to open

DialogPick(title='Pick', message='', names=[], values=[], sort=False,
index=0) -- Pick from a single-selection listbox

DialogSaveFile(title='Save', value='', wildcard='All files (*.*)|*.*')
-- Specify a file to save

DialogShow(title='Show', message='') -- Show a message

Content of

import lbc

labels = 'Label1 Label2 Label3'.split()
names = 'Name1 Name2 Name3'.split()
values = 'Value1 Value2 Value3'.split()

result = lbc.DialogChoose(title='Choose a Button', message='My message',
names=['Button1', 'Button2', 'Button3'])
lbc.DialogShow(title='result', message=result)

result = lbc.DialogConfirm(title='Confirm an Action', message='my
question', value='Y')
lbc.DialogShow(title='result', message=result)

result=lbc.DialogInput(title='Input a Value', label='My Label',
value='My Value')
lbc.DialogShow(title='result', message=result)

result = lbc.DialogMemo(title='Input Multiple Lines', label='',
lbc.DialogShow(title='result', message=result)

result = lbc.DialogMultiInput(title='Input Multiple Values',
labels=labels, values=values)
lbc.DialogShow(title='result', message=result)

result = lbc.DialogPick(title='Pick an Item', names=names,
values=values, sort=True)
lbc.DialogShow(title='result', message=result)

result=lbc.DialogMultiPick(title='Pick Multiple Items', names=names,
lbc.DialogShow(title='result', message=result)

result = lbc.DialogOpenFile(value=r'c:\temp\test.txt')
lbc.DialogShow(title='result', message=result)

result = lbc.DialogSaveFile(value=r'c:\temp\test.txt')
lbc.DialogShow(title='result', message=result)

result = lbc.DialogBrowseForFolder(value=r'c:\temp')
lbc.DialogShow(title='result', message=result)

Content of

import wx, lbc

# Custom event handler
def OnEvent(dlg, event, name):
        if lbc.IsCloseEvent(event):
                if lbc.DialogConfirm(title='Confirm', message='Exit program?',
value='Y') == 'Y': return event.Skip()
                else: return event.Veto()

        txt = dlg.Controls['TextCtrl_Fruit']
        lst = dlg.Controls['ListBox_Basket']
        if not lbc.IsClickEvent(event): pass
        elif name == 'Button_Add':
                fruit = txt.GetValue()
                if len(fruit) == 0: return lbc.DialogShow(title='Alert', 
fruit to add!')
                index = lst.GetCount() - 1
        elif name == 'Button_Delete':
                index = lst.GetSelection()
                if index == -1: return lbc.DialogShow(title='Alert', 
message='No fruit
to delete!')
                if index == lst.GetCount(): index -= 1
                if index>= 0: lst.SetSelection(index)

# Main program
app = lbc.App()
dlg = lbc.Dialog(title='Fruit Basket')
dlg.Complete(buttons=['Add', 'Delete'], handler=OnEvent)

View the list's information and change your settings at

View the list's information and change your settings at

Other related posts: