Grid and scrollbars

BH BH at bh.com
Mon Mar 26 11:15:39 CEST 2007


Hi,

I am trying to build my own IHM with two treeCtrl and 1 grid, based on 
the wx.aui demos.
My problem is with the Grid. It dosen't have scrollbars.
I tried many methods (fit) but it always fail.

Can somebody point me where is the mistake ?

Thx

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
import wx.aui
import wx.grid
import wx.html
import sys,os
import images

# stuff for debugging
print "wx.version:", wx.version()
print "pid:", os.getpid()
##raw_input("Press Enter...")

assertMode = wx.PYAPP_ASSERT_DIALOG
##assertMode = wx.PYAPP_ASSERT_EXCEPTION

text = """\
Hello World! TXT
"""

overview = """\
<html><body>
<h3>Hello World ! HTML</h3>

</body></html>
"""


class MyFrame(wx.Frame):
     def __init__(self, parent, id=-1, title='wx.aui Test',
                  size=(1024, 768), style=wx.DEFAULT_FRAME_STYLE):
         wx.Frame.__init__(self, parent, id, title, (0,0), size, style)

         self._mgr = wx.aui.AuiManager(self)

         self.data = [[1010, "The foo doesn't bar", "major", 1, 'MSW', 
1, 1, 1, 1.12],
             [1011, "I've got a wicket in my wocket", "wish list", 2, 
'other', 0, 0, 0, 1.50],
             [1012, "Rectangle() returns a triangle", "critical", 5, 
'all', 0, 0, 0, 1.56]
             ]


         self.grid = CustTableGrid(self, self.data)

         mb = wx.MenuBar()

         file_menu = wx.Menu()
         file_menu.Append(wx.ID_EXIT, "Exit")
         mb.Append(file_menu, "File")
         self.SetMenuBar(mb)

         self.statusbar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)
         self.statusbar.SetStatusWidths([-2, -3])
         self.statusbar.SetStatusText("Ready", 0)
         self.statusbar.SetStatusText("Welcome To wxPython!", 1)


         tb2 = wx.ToolBar(self, -1, wx.DefaultPosition, wx.DefaultSize,
                          wx.TB_FLAT | wx.TB_NODIVIDER)
         tb2.SetToolBitmapSize(wx.Size(16,16))
         tb2_bmp1 = wx.ArtProvider_GetBitmap(wx.ART_QUESTION, 
wx.ART_OTHER, wx.Size(16, 16))
         tb2.AddLabelTool(101, "Test", tb2_bmp1)
         tb2.AddLabelTool(101, "Test", tb2_bmp1)
         tb2.AddLabelTool(101, "Test", tb2_bmp1)
         tb2.AddLabelTool(101, "Test", tb2_bmp1)
         tb2.AddSeparator()
         tb2.AddLabelTool(101, "Test", tb2_bmp1)
         tb2.AddLabelTool(101, "Test", tb2_bmp1)
         tb2.Realize()

         #Toolbar
         self._mgr.AddPane(tb2, wx.aui.AuiPaneInfo().
                           Name("tb2").Caption("Toolbar 2").
                           ToolbarPane().Top().Row(1).
                           LeftDockable(False).RightDockable(False))
         #Grid
         self._mgr.AddPane(self.grid, wx.aui.AuiPaneInfo().
                           Name("Matrice").Caption("Matrice").
 
Center().Layer(1).Position(1).CloseButton(True).MaximizeButton(True))
         #Tree Left
         self._mgr.AddPane(self.CreateTreeCtrl(), wx.aui.AuiPaneInfo().
                           Name("Q").Caption("Tree Pane").
 
Left().Layer(1).Position(1).CloseButton(True).MaximizeButton(True))
         #Tree Right
         self._mgr.AddPane(self.CreateTreeCtrl(), wx.aui.AuiPaneInfo().
                           Name("Quest").Caption("Tree Pane").
 
Right().Layer(1).Position(1).CloseButton(True).MaximizeButton(True))



         self._mgr.Update()

         self.Bind(wx.EVT_CLOSE, self.OnClose)
         self.Bind(wx.EVT_MENU, self.OnClose, id=wx.ID_EXIT)

     def CreateTreeCtrl(self):

         tree = wx.TreeCtrl(self, -1, wx.Point(0, 0), wx.Size(160, 250),
                            wx.TR_DEFAULT_STYLE | wx.NO_BORDER)


         root = tree.AddRoot("Root")
         items = []

         imglist = wx.ImageList(16, 16, True, 2)
         imglist.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER, 
wx.ART_OTHER, wx.Size(16,16)))
         imglist.Add(wx.ArtProvider_GetBitmap(wx.ART_NORMAL_FILE, 
wx.ART_OTHER, wx.Size(16,16)))
         tree.AssignImageList(imglist)


         tree.AppendItem(root, "Root", 1)
         items.append(tree.AppendItem(root, "Item 1", 0))
         items.append(tree.AppendItem(root, "Item 2", 0))
         items.append(tree.AppendItem(root, "Item 3", 0))
         items.append(tree.AppendItem(root, "Item 4", 0))
         items.append(tree.AppendItem(root, "Item 5", 0))

         for ii in xrange(len(items)):

             id = items[ii]
             tree.AppendItem(id, "Subitem 1", 1)
             tree.AppendItem(id, "Subitem 2", 1)
             tree.AppendItem(id, "Subitem 3", 1)
             tree.AppendItem(id, "Subitem 4", 1)
             tree.AppendItem(id, "Subitem 5", 1)

         tree.Expand(root)

         return tree

     def CreateHTMLCtrl(self):
         ctrl = wx.html.HtmlWindow(self, -1, wx.DefaultPosition, 
wx.Size(400, 300))
         if "gtk2" in wx.PlatformInfo:
             ctrl.SetStandardFonts()
         ctrl.SetPage(self.GetIntroText())
         return ctrl

     def GetIntroText(self):
         return overview

     def OnClose(self, event):
         # deinitialize the frame manager
         self._mgr.UnInit()
         # delete the frame
         self.Destroy()


class CustomDataTable(wx.grid.PyGridTableBase):
     def __init__(self,data):
         self.data= data
         wx.grid.PyGridTableBase.__init__(self)
         self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 
'Platform',
                           'Opened?', 'Fixed?', 'Tested?', 'TestFloat']
         self.dataTypes = [wx.grid.GRID_VALUE_NUMBER,
                           wx.grid.GRID_VALUE_STRING,
                           wx.grid.GRID_VALUE_CHOICE + ':only in a 
million years!,wish list,minor,normal,major,critical',
                           wx.grid.GRID_VALUE_NUMBER + ':1,5',
                           wx.grid.GRID_VALUE_CHOICE + ':all,MSW,GTK,other',
                           wx.grid.GRID_VALUE_BOOL,
                           wx.grid.GRID_VALUE_BOOL,
                           wx.grid.GRID_VALUE_BOOL,
                           wx.grid.GRID_VALUE_FLOAT + ':6,2',
                           ]
     def GetNumberRows(self):
         return len(self.data) + 1
     def GetNumberCols(self):
         return len(self.data[0])
     def IsEmptyCell(self, row, col):
         try:
             return not self.data[row][col]
         except IndexError:
             return True
     def GetValue(self, row, col):
         try:
             return self.data[row][col]
         except IndexError:
             return ''
     def SetValue(self, row, col, value):
         try:
             self.data[row][col] = value
         except IndexError:
             self.data.append([''] * self.GetNumberCols())
             self.SetValue(row, col, value)
             msg = wx.grid.GridTableMessage(self, 
       # The table
 
wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
                                      1) 
# how many
             self.GetView().ProcessTableMessage(msg)
             self.GetView().MoveCursorDown(false)
     def GetColLabelValue(self, col):
         return self.colLabels[col]
     def GetTypeName(self, row, col):
         return self.dataTypes[col]
     def CanGetValueAs(self, row, col, typeName):
         colType = self.dataTypes[col].split(':')[0]
         if typeName == colType:
             return True
         else:
             return False
     def CanSetValueAs(self, row, col, typeName):
         return self.CanGetValueAs(row, col, typeName)

class CustTableGrid(wx.grid.Grid):
     def __init__(self, parent, data):
         wx.grid.Grid.__init__(self, parent, -1)
         self.table = CustomDataTable(data)
         self.SetTable(self.table, True)
         self.SetRowLabelSize(0)
         self.SetMargins(0,0)
         self.AutoSizeColumns(False)
         wx.grid.EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick)


         #return self.SetTable
     def OnLeftDClick(self, evt):
         if self.CanEnableCellControl():
             self.EnableCellEditControl()

def TestProgram():
     app = wx.App(0)

     frame = MyFrame(None)
     frame.Show()
     app.MainLoop()

if __name__ == '__main__':
     TestProgram()



More information about the Python-list mailing list