The demo is incomplete but what you find here should be working code. You are welcome to modify it by suggesting changes or putting diffs at the bottom or if you want to work more interactively e-mail me mailto:pac1@tiac.net We may want to move "development" of this demo to something like sourceforge.

A word to the wise: This is a wiki. That means anyone can change the code on this page. Run it at your own peril. Read every line of it to make sure no-one has monkeyed with it. We'll try to come up with a secure code repository soon. Till then, this page is just to try out the demo idea.

# DemoA.py will be a "complete" python application. It is currently under construction.
#
# by complete, I mean that the demo will have all the features of a robust wxPython application 
# with one exception... It won't attempt to actually be a real application.  
# Its data model and structures will be artificial
#
# When finished, all the things a real application should have will be there.
#
# Menus, Toolbars, Status Bar, Persistent Data, Data Creation and Editing etc.

# At this point, only the UI is anywhere near complete.
#
# Features:
#
#        Menu with Exit Menu Item
#        Toobar with Add Item and Add Item-Detail buttons
#        Content area with panels containing Items and Item Details
#        Status Bar with Time Display updated every second
#
#        Add Item button adds a panel containing widgets for
#        attributes of an "item"
#
#        Add Item-Detail adds widgets to the Item Panel containing
#        repeatable attributes of the item in the panel
#
#        Note: common uses for the Item and Item-detail application model are
#              invoices, inventories, account with transactions
#              etc.

# To Do: Calculate the scrollbars parameters correctly
#        Update a data object with changes from screen
#        Save data in a file
#        Read saved data from a file and re-display the items
#        Enable selection of current item using mouse
#        Enable selection of previous or next item using a key
#        Enable tabbing between widgets that make up an item
#        use XML for storing and retrieving data from a file
#
# Would be nice to do:
#        Use xml to describe and construct the user interface

import wx

import UserList
import time

def menuItem( app, menu, label, command, help):
    id=wx.NewId()
    menu.Append(id, label, help )
    wx.EVT_MENU( app, id, command)
def menuSeparator(app, menu):
    menu.AppendSeparator()

class UiMenus:
    def __init__(self,parent,controller):
        c=controller
        parent.mainmenu = wx.MenuBar()
        file = wx.Menu()
        menuItem(parent, file, '&Exit',           c.fileExit,       '')
        parent.mainmenu.Append (file, '&File')
        parent.SetMenuBar (parent.mainmenu) # Attach the menu bar to window.

class UiToolBars:
    def __init__(self,app,controller):
        c=controller
        tb = app.CreateToolBar(wx.TB_HORIZONTAL|wx.NO_BORDER)
        id = wx.NewId()
        tb.AddControl(wx.Button(
            tb, id,  'New Panel', wx.Point(0,0),wx.Size(80,20 )))
        wx.EVT_BUTTON(app, id, c.toolNewPanel)
        id = wx.NewId()
        tb.AddControl(wx.Button(
            tb, id,  'New ItemDetails',  wx.Point(0,20),wx.Size(80,20)))
        wx.EVT_BUTTON(app, id, c.toolNewPanelItemDetails)

        tb.Realize()

class UiStatusBar:
    def __init__(self,parent):
        self.parent=parent
        sb = parent.CreateStatusBar(2)
        parent.SetStatusText("This is the statusbar",0)
        sb.SetStatusWidths([-1, 150])
        self.timer = wx.PyTimer(self.Notify)
        self.timer.Start(1000)
        self.Notify()

    def Notify(self):
        t = time.localtime(time.time())
        st = time.strftime(" %d-%b-%Y   %I:%M:%S", t)
        self.parent.SetStatusText(st, 1)

    def __del__(self):
        self.timer.Stop()
        del self.timer

class ItemDetails:
    def __init__(self):
        self.data=''

class Item:
    def __init__(self):
        self.description = ''
        self.sessionsList=[]

    def update ( self, description):
        self.description = description

    def addItemDetails( self, session):
        self.sessionsList.add(session)

class Data:
    def __init__(self):
        self.ItemList=ItemList()
class Tools:

    def __init__(self,data):
        self.data=data
        self.ui=None

    def setUi(self, ui):
        self.ui = ui

    def fileExit(self, event):
        print "FileExit Not implemented"
        print self
        print event

    def toolNewPanel(self, event):
        newItem=Item()
        self.ui.content.addItem(newItem)

    def toolNewPanelItemDetails(self, event):
        newPanelItemDetails=None
        self.ui.content.addItemDetails(newPanelItemDetails)


class ItemDetails:
    def __init__(self,parentWindow,parentSizer,item=None):


        self.sizer= wx.BoxSizer(wx.HORIZONTAL)

        self.description   = wx.TextCtrl(parentWindow, -1,
                                        'detail description',
                                        wx.Point(-1,-1), wx.Size(150,20),
                                        0, wx.DefaultValidator)

        self.sizer.Add(self.description,0)
        parentSizer.Add(self.sizer)

class ItemDetailsList:

    def __init__(self,parent,parentSizer,item=None):
        self.parentWindow=parent
        self.sessionList = []
        self.sizer  = wx.BoxSizer(wx.VERTICAL)
        parentSizer.Add(self.sizer)

class ItemFrame(wx.Panel):
    def __init__(self,parent,id=-1,item=None):
        wx.Panel.__init__(self, parent, id, wx.Point(-1,-1), wx.Size(800,60),
                         wx.SUNKEN_BORDER)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)
        self.description = wx.TextCtrl(self, -1,  'description',
                                      wx.Point(-1,-1), wx.Size(450,60),
                                      0, wx.DefaultValidator)
        id=wx.NewId()

        self.sizer.Add(self.description,0)
        self.itemDetailList = ItemDetailsList(self,self.sizer,item)

        self.SetAutoLayout(1)
        self.sizer.Layout()

class UiContent(wx.ScrolledWindow):
    def __init__(self,app,id,data=None):
        wx.ScrolledWindow.__init__(self,app, -1)
        self.parent=app
        self.panels = []
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.Show(True)
        self.SetScrollbars(5,5,50,0)
        self.currentItem=None
        self.n=0

    def addItem(self,item=None):
        id=wx.NewId()

        self.currentItem = f =ItemFrame(self,id,item)
        self.n=self.n+1
        self.sizer.Add(f,0, wx.EXPAND)
        self.panels.append(f)
        self.Layout()

        self.SetClientSize(self.parent.GetClientSize())
        self.setScrollbars


    def addItemDetails(self,itemDetail=None):
        panel = self.currentItem
        psizer = panel.GetSizer()
        dsizer = wx.BoxSizer(wx.HORIZONTAL)
        id   = wx.TextCtrl(panel, -1, '1',
                          size=(20,20))
        desc = wx.TextCtrl(panel, -1, 'detail description',
                          size=(150,20))
        dsizer.Add(id)
        dsizer.Add(desc)
        psizer.Add(dsizer)
        psizer.Fit(panel)
        self.sizer.SetItemMinSize(panel, panel.GetSize().width,
                                  panel.GetSize().height)
        self.Layout()


    def setScrollbars(self):
        self.SetScrollbars(5,5,20,self.n*39)

class Ui(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, -1, title, size = (500, 600),
                         style=wx.DEFAULT_FRAME_STYLE)
    def start(self, data, controller):
        self.menus      = UiMenus(self,controller)
        self.ToolBars   = UiToolBars(self,controller)
        self.statusBar  = UiStatusBar(self)
        self.content    = UiContent(self,data)
        self.Show(True)

class SizersDemo(wx.App):
    def OnInit(self):
        self.data = None
        self.controller=Tools(self.data)
        self.ui = Ui(None, -1, "SizersDemo")
        self.controller.setUi(self.ui)
        self.ui.start(self.data, self.controller)
        self.SetTopWindow(self.ui)
        return True

if __name__ == "__main__":
    sizersDemo = SizersDemo(0)
    sizersDemo.MainLoop()

Suggest Changes here

DemoA.py (last edited 2010-01-01 09:08:51 by c-98-248-43-159)

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