Grid problem
"Werner F. Bruhin" <[email protected]>
| Newsgroups | gmane.comp.ide.boa-constructor.user |
|---|---|
| Message-ID | <[email protected]> |
Giorgio, I respond here as I can attach files, i.e. they won't get mangled as they do on sourceforge. If you create your own class for e.g. a Grid and you want to use it in the Boa designer you have to respect/use all the init arguments, you missed out "name". Attached is a sample which runs. Also note that I but the script encoding line in, this is important if you ever want to run this script on other machines and/or you want to use gettext to translate the app. Werner ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Register now and save $200. Hurry, offer ends at 11:59 p.m., Monday, April 7! Use priority code J8TLD2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone _______________________________________________ Boa-constructor-users mailing list Boa-constructor-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/boa-constructor-users
gsFrame.py
(text/plain, 5.7 KB)
# -*- coding: iso-8859-1 -*-#
#-----------------------------------------------------------------------------
# Name: gsFrame.py
# Purpose:
#
# Created: 2008/04/06
# RCS-ID: $Id: gsFrame.py $
#-----------------------------------------------------------------------------
#Boa:Frame:Frame1
import wx
import wx.grid
def create(parent):
return Frame1(parent)
[wxID_FRAME1, wxID_FRAME1GRID1, wxID_FRAME1RADIOBUTTON1,
] = [wx.NewId() for _init_ctrls in range(3)]
class Frame1(wx.Frame):
def _init_ctrls(self, prnt):
# generated method, don't edit
wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
pos=wx.Point(255, 228), size=wx.Size(566, 339),
style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
self.SetClientSize(wx.Size(558, 305))
self.grid1 = MyGrid(id=wxID_FRAME1GRID1, name='grid1',
parent=self, pos=wx.Point(40, 64), size=wx.Size(232, 177),
style=0)
self.radioButton1 = wx.RadioButton(id=wxID_FRAME1RADIOBUTTON1,
label='radioButton1', name='radioButton1', parent=self,
pos=wx.Point(352, 80), size=wx.Size(81, 13), style=0)
def __init__(self, parent):
self._init_ctrls(parent)
class MyGrid(wx.grid.Grid):
""" A Copy&Paste enabled grid class"""
def __init__(self, parent, id, pos, size, style, name):
wx.grid.Grid.__init__(self, parent, id, pos, size, style, name)
wx.EVT_KEY_DOWN(self, self.OnKey)
def selection(self):
# Show cell selection
# If selection is cell...
if self.GetSelectedCells():
print "Selected cells " + str(self.GetSelectedCells())
# If selection is block...
if self.GetSelectionBlockTopLeft():
print "Selection block top left " + str(self.GetSelectionBlockTopLeft())
if self.GetSelectionBlockBottomRight():
print "Selection block bottom right " + str(self.GetSelectionBlockBottomRight())
# If selection is col...
if self.GetSelectedCols():
print "Selected cols " + str(self.GetSelectedCols())
# If selection is row...
if self.GetSelectedRows():
print "Selected rows " + str(self.GetSelectedRows())
def currentcell(self):
# Show cursor position
row = self.GetGridCursorRow()
col = self.GetGridCursorCol()
cell = (row, col)
print "Current cell " + str(cell)
def OnKey(self, event):
# If Ctrl+C is pressed...
if event.ControlDown() and event.GetKeyCode() == 67:
print "Ctrl+C"
self.selection()
# Call copy method
self.copy()
# If Ctrl+V is pressed...
if event.ControlDown() and event.GetKeyCode() == 86:
print "Ctrl+V"
self.currentcell()
# Call paste method
self.paste()
# If Supr is presed
if event.GetKeyCode() == 127:
print "Supr"
# Call delete method
self.delete()
# Skip other Key events
if event.GetKeyCode():
event.Skip()
return
def copy(self):
print "Copy method"
# Number of rows and cols
rows = self.GetSelectionBlockBottomRight()[0][0] - self.GetSelectionBlockTopLeft()[0][0] + 1
cols = self.GetSelectionBlockBottomRight()[0][1] - self.GetSelectionBlockTopLeft()[0][1] + 1
# data variable contain text that must be set in the clipboard
data = ''
# For each cell in selected range append the cell value in the data variable
# Tabs '\t' for cols and '\r' for rows
for r in range(rows):
for c in range(cols):
data = data + str(self.GetCellValue(self.GetSelectionBlockTopLeft()[0][0] + r, self.GetSelectionBlockTopLeft()[0][1] + c))
if c < cols - 1:
data = data + '\t'
data = data + '\n'
# Create text data object
clipboard = wx.TextDataObject()
# Set data object value
clipboard.SetText(data)
# Put the data in the clipboard
if wx.TheClipboard.Open():
wx.TheClipboard.SetData(clipboard)
wx.TheClipboard.Close()
else:
wx.MessageBox("Can't open the clipboard", "Error")
def paste(self):
print "Paste method"
clipboard = wx.TextDataObject()
if wx.TheClipboard.Open():
wx.TheClipboard.GetData(clipboard)
wx.TheClipboard.Close()
else:
wx.MessageBox("Can't open the clipboard", "Error")
data = clipboard.GetText()
table = []
y = -1
# Convert text in a array of lines
for r in data.splitlines():
y = y +1
x = -1
# Convert c in a array of text separated by tab
for c in r.split('\t'):
x = x +1
self.SetCellValue(self.GetGridCursorRow() + y, self.GetGridCursorCol() + x, c)
def delete(self):
print "Delete method"
# Number of rows and cols
rows = self.GetSelectionBlockBottomRight()[0][0] - self.GetSelectionBlockTopLeft()[0][0] + 1
cols = self.GetSelectionBlockBottomRight()[0][1] - self.GetSelectionBlockTopLeft()[0][1] + 1
# Clear cells contents
for r in range(rows):
for c in range(cols):
self.SetCellValue(self.GetSelectionBlockTopLeft()[0][0] + r, self.GetSelectionBlockTopLeft()[0][1] + c, '')
gsApp.py
(text/plain, 694 B)
# -*- coding: iso-8859-1 -*-#
#-----------------------------------------------------------------------------
# Name: gsApp.py
# Purpose:
#
# Created: 2008/04/06
# RCS-ID: $Id: gsApp.py $
#-----------------------------------------------------------------------------
#!/usr/bin/env python
#Boa:App:BoaApp
import wx
import gsFrame
modules ={'Frame1': [1, 'Main frame of Application', 'Frame1.py']}
class BoaApp(wx.App):
def OnInit(self):
self.main = gsFrame.create(None)
self.main.Show()
self.SetTopWindow(self.main)
return True
def main():
application = BoaApp(0)
application.MainLoop()
if __name__ == '__main__':
main()