Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
ss1.SheetGUI Class Reference

List of all members.

Public Member Functions

def __init__
def delete_event
def escape_event
def load_entry
def makegrid
def selectall
def selectcolumn
def extendcolumn
def selectrow
def extendrow
def press
def motion
def whichxy
def save
def setcurrent
def setcorner
def setbeacon
def clearfocus
def return_event
def shift_return_event
def tab_event
def shift_tab_event
def change_cell
def sync

Public Attributes

 filename
 sheet
 root
 beacon
 entry
 savebutton
 cellgrid
 currentxy
 cornerxy
 rows
 columns
 gridcells

Static Public Attributes

 release = motion

Detailed Description

Beginnings of a GUI for a spreadsheet.

TO DO:
- clear multiple cells
- Insert, clear, remove rows or columns
- Show new contents while typing
- Scroll bars
- Grow grid when window is grown
- Proper menus
- Undo, redo
- Cut, copy and paste
- Formatting and alignment

Definition at line 491 of file ss1.py.


Constructor & Destructor Documentation

def ss1.SheetGUI.__init__ (   self,
  filename = "sheet1.xml",
  rows = 10,
  columns = 5 
)
Constructor.

Load the sheet from the filename argument.
Set up the Tk widget tree.

Definition at line 507 of file ss1.py.

00507 
00508     def __init__(self, filename="sheet1.xml", rows=10, columns=5):
00509         """Constructor.
00510 
00511         Load the sheet from the filename argument.
00512         Set up the Tk widget tree.
00513         """
00514         # Create and load the sheet
00515         self.filename = filename
00516         self.sheet = Sheet()
00517         if os.path.isfile(filename):
00518             self.sheet.load(filename)
00519         # Calculate the needed grid size
00520         maxx, maxy = self.sheet.getsize()
00521         rows = max(rows, maxy)
00522         columns = max(columns, maxx)
00523         # Create the widgets
00524         self.root = Tk.Tk()
00525         self.root.wm_title("Spreadsheet: %s" % self.filename)
00526         self.beacon = Tk.Label(self.root, text="A1",
00527                                font=('helvetica', 16, 'bold'))
00528         self.entry = Tk.Entry(self.root)
00529         self.savebutton = Tk.Button(self.root, text="Save",
00530                                     command=self.save)
00531         self.cellgrid = Tk.Frame(self.root)
00532         # Configure the widget lay-out
00533         self.cellgrid.pack(side="bottom", expand=1, fill="both")
00534         self.beacon.pack(side="left")
00535         self.savebutton.pack(side="right")
00536         self.entry.pack(side="left", expand=1, fill="x")
00537         # Bind some events
00538         self.entry.bind("<Return>", self.return_event)
00539         self.entry.bind("<Shift-Return>", self.shift_return_event)
00540         self.entry.bind("<Tab>", self.tab_event)
00541         self.entry.bind("<Shift-Tab>", self.shift_tab_event)
00542         self.entry.bind("<Delete>", self.delete_event)
00543         self.entry.bind("<Escape>", self.escape_event)
00544         # Now create the cell grid
00545         self.makegrid(rows, columns)
00546         # Select the top-left cell
00547         self.currentxy = None
00548         self.cornerxy = None
00549         self.setcurrent(1, 1)
00550         # Copy the sheet cells to the GUI cells
00551         self.sync()

Here is the caller graph for this function:


Member Function Documentation

def ss1.SheetGUI.change_cell (   self)

Definition at line 771 of file ss1.py.

00771 
00772     def change_cell(self):
00773         "Set the current cell from the entry widget."
00774         x, y = self.currentxy
00775         text = self.entry.get()
00776         cell = None
00777         if text.startswith('='):
00778             cell = FormulaCell(text[1:])
00779         else:
00780             for cls in int, int, float, complex:
00781                 try:
00782                     value = cls(text)
00783                 except:
00784                     continue
00785                 else:
00786                     cell = NumericCell(value)
00787                     break
00788         if cell is None and text:
00789             cell = StringCell(text)
00790         if cell is None:
00791             self.sheet.clearcell(x, y)
00792         else:
00793             self.sheet.setcell(x, y, cell)
00794         self.sync()

Here is the call graph for this function:

Here is the caller graph for this function:

def ss1.SheetGUI.clearfocus (   self)

Definition at line 731 of file ss1.py.

00731 
00732     def clearfocus(self):
00733         if self.currentxy is not None:
00734             x1, y1 = self.currentxy
00735             x2, y2 = self.cornerxy or self.currentxy
00736             if x1 > x2:
00737                 x1, x2 = x2, x1
00738             if y1 > y2:
00739                 y1, y2 = y2, y1
00740             for (x, y), cell in self.gridcells.items():
00741                 if x1 <= x <= x2 and y1 <= y <= y2:
00742                     cell['bg'] = 'white'

Here is the caller graph for this function:

def ss1.SheetGUI.delete_event (   self,
  event 
)

Definition at line 552 of file ss1.py.

00552 
00553     def delete_event(self, event):
00554         if self.cornerxy != self.currentxy and self.cornerxy is not None:
00555             self.sheet.clearcells(*(self.currentxy + self.cornerxy))
00556         else:
00557             self.sheet.clearcell(*self.currentxy)
00558         self.sync()
00559         self.entry.delete(0, 'end')
00560         return "break"

Here is the call graph for this function:

def ss1.SheetGUI.escape_event (   self,
  event 
)

Definition at line 561 of file ss1.py.

00561 
00562     def escape_event(self, event):
00563         x, y = self.currentxy
00564         self.load_entry(x, y)

Here is the call graph for this function:

def ss1.SheetGUI.extendcolumn (   self,
  event 
)

Definition at line 636 of file ss1.py.

00636 
00637     def extendcolumn(self, event):
00638         x, y = self.whichxy(event)
00639         if x > 0:
00640             self.setcurrent(self.currentxy[0], 1)
00641             self.setcorner(x, sys.maxint)

Here is the call graph for this function:

def ss1.SheetGUI.extendrow (   self,
  event 
)

Definition at line 647 of file ss1.py.

00647 
00648     def extendrow(self, event):
00649         x, y = self.whichxy(event)
00650         if y > 0:
00651             self.setcurrent(1, self.currentxy[1])
00652             self.setcorner(sys.maxint, y)

Here is the call graph for this function:

def ss1.SheetGUI.load_entry (   self,
  x,
  y 
)

Definition at line 565 of file ss1.py.

00565 
00566     def load_entry(self, x, y):
00567         cell = self.sheet.getcell(x, y)
00568         if cell is None:
00569             text = ""
00570         elif isinstance(cell, FormulaCell):
00571             text = '=' + cell.formula
00572         else:
00573             text, alignment = cell.format()
00574         self.entry.delete(0, 'end')
00575         self.entry.insert(0, text)
00576         self.entry.selection_range(0, 'end')

Here is the caller graph for this function:

def ss1.SheetGUI.makegrid (   self,
  rows,
  columns 
)
Helper to create the grid of GUI cells.

The edge (x==0 or y==0) is filled with labels; the rest is real cells.

Definition at line 577 of file ss1.py.

00577 
00578     def makegrid(self, rows, columns):
00579         """Helper to create the grid of GUI cells.
00580 
00581         The edge (x==0 or y==0) is filled with labels; the rest is real cells.
00582         """
00583         self.rows = rows
00584         self.columns = columns
00585         self.gridcells = {}
00586         # Create the top left corner cell (which selects all)
00587         cell = Tk.Label(self.cellgrid, relief='raised')
00588         cell.grid_configure(column=0, row=0, sticky='NSWE')
00589         cell.bind("<ButtonPress-1>", self.selectall)
00590         # Create the top row of labels, and configure the grid columns
00591         for x in range(1, columns+1):
00592             self.cellgrid.grid_columnconfigure(x, minsize=64)
00593             cell = Tk.Label(self.cellgrid, text=colnum2name(x), relief='raised')
00594             cell.grid_configure(column=x, row=0, sticky='WE')
00595             self.gridcells[x, 0] = cell
00596             cell.__x = x
00597             cell.__y = 0
00598             cell.bind("<ButtonPress-1>", self.selectcolumn)
00599             cell.bind("<B1-Motion>", self.extendcolumn)
00600             cell.bind("<ButtonRelease-1>", self.extendcolumn)
00601             cell.bind("<Shift-Button-1>", self.extendcolumn)
00602         # Create the leftmost column of labels
00603         for y in range(1, rows+1):
00604             cell = Tk.Label(self.cellgrid, text=str(y), relief='raised')
00605             cell.grid_configure(column=0, row=y, sticky='WE')
00606             self.gridcells[0, y] = cell
00607             cell.__x = 0
00608             cell.__y = y
00609             cell.bind("<ButtonPress-1>", self.selectrow)
00610             cell.bind("<B1-Motion>", self.extendrow)
00611             cell.bind("<ButtonRelease-1>", self.extendrow)
00612             cell.bind("<Shift-Button-1>", self.extendrow)
00613         # Create the real cells
00614         for x in range(1, columns+1):
00615             for y in range(1, rows+1):
00616                 cell = Tk.Label(self.cellgrid, relief='sunken',
00617                                 bg='white', fg='black')
00618                 cell.grid_configure(column=x, row=y, sticky='NSWE')
00619                 self.gridcells[x, y] = cell
00620                 cell.__x = x
00621                 cell.__y = y
00622                 # Bind mouse events
00623                 cell.bind("<ButtonPress-1>", self.press)
00624                 cell.bind("<B1-Motion>", self.motion)
00625                 cell.bind("<ButtonRelease-1>", self.release)
00626                 cell.bind("<Shift-Button-1>", self.release)

def ss1.SheetGUI.motion (   self,
  event 
)

Definition at line 658 of file ss1.py.

00658 
00659     def motion(self, event):
00660         x, y = self.whichxy(event)
00661         if x > 0 and y > 0:
00662             self.setcorner(x, y)

Here is the call graph for this function:

def ss1.SheetGUI.press (   self,
  event 
)

Definition at line 653 of file ss1.py.

00653 
00654     def press(self, event):
00655         x, y = self.whichxy(event)
00656         if x > 0 and y > 0:
00657             self.setcurrent(x, y)

Here is the call graph for this function:

def ss1.SheetGUI.return_event (   self,
  event 
)

Definition at line 743 of file ss1.py.

00743 
00744     def return_event(self, event):
00745         "Callback for the Return key."
00746         self.change_cell()
00747         x, y = self.currentxy
00748         self.setcurrent(x, y+1)
00749         return "break"

Here is the call graph for this function:

def ss1.SheetGUI.save (   self)

Definition at line 674 of file ss1.py.

00674 
00675     def save(self):
00676         self.sheet.save(self.filename)

def ss1.SheetGUI.selectall (   self,
  event 
)

Definition at line 627 of file ss1.py.

00627 
00628     def selectall(self, event):
00629         self.setcurrent(1, 1)
00630         self.setcorner(sys.maxint, sys.maxint)

Here is the call graph for this function:

def ss1.SheetGUI.selectcolumn (   self,
  event 
)

Definition at line 631 of file ss1.py.

00631 
00632     def selectcolumn(self, event):
00633         x, y = self.whichxy(event)
00634         self.setcurrent(x, 1)
00635         self.setcorner(x, sys.maxint)

Here is the call graph for this function:

def ss1.SheetGUI.selectrow (   self,
  event 
)

Definition at line 642 of file ss1.py.

00642 
00643     def selectrow(self, event):
00644         x, y = self.whichxy(event)
00645         self.setcurrent(1, y)
00646         self.setcorner(sys.maxint, y)

Here is the call graph for this function:

def ss1.SheetGUI.setbeacon (   self,
  x1,
  y1,
  x2,
  y2 
)

Definition at line 711 of file ss1.py.

00711 
00712     def setbeacon(self, x1, y1, x2, y2):
00713         if x1 == y1 == 1 and x2 == y2 == sys.maxint:
00714             name = ":"
00715         elif (x1, x2) == (1, sys.maxint):
00716             if y1 == y2:
00717                 name = "%d" % y1
00718             else:
00719                 name = "%d:%d" % (y1, y2)
00720         elif (y1, y2) == (1, sys.maxint):
00721             if x1 == x2:
00722                 name = "%s" % colnum2name(x1)
00723             else:
00724                 name = "%s:%s" % (colnum2name(x1), colnum2name(x2))
00725         else:
00726             name1 = cellname(*self.currentxy)
00727             name2 = cellname(*self.cornerxy)
00728             name = "%s:%s" % (name1, name2)
00729         self.beacon['text'] = name
00730 

Here is the call graph for this function:

Here is the caller graph for this function:

def ss1.SheetGUI.setcorner (   self,
  x,
  y 
)

Definition at line 691 of file ss1.py.

00691 
00692     def setcorner(self, x, y):
00693         if self.currentxy is None or self.currentxy == (x, y):
00694             self.setcurrent(x, y)
00695             return
00696         self.clearfocus()
00697         self.cornerxy = x, y
00698         x1, y1 = self.currentxy
00699         x2, y2 = self.cornerxy or self.currentxy
00700         if x1 > x2:
00701             x1, x2 = x2, x1
00702         if y1 > y2:
00703             y1, y2 = y2, y1
00704         for (x, y), cell in self.gridcells.items():
00705             if x1 <= x <= x2 and y1 <= y <= y2:
00706                 cell['bg'] = 'lightBlue'
00707         gridcell = self.gridcells.get(self.currentxy)
00708         if gridcell is not None:
00709             gridcell['bg'] = 'yellow'
00710         self.setbeacon(x1, y1, x2, y2)

Here is the call graph for this function:

Here is the caller graph for this function:

def ss1.SheetGUI.setcurrent (   self,
  x,
  y 
)

Definition at line 677 of file ss1.py.

00677 
00678     def setcurrent(self, x, y):
00679         "Make (x, y) the current cell."
00680         if self.currentxy is not None:
00681             self.change_cell()
00682         self.clearfocus()
00683         self.beacon['text'] = cellname(x, y)
00684         self.load_entry(x, y)
00685         self.entry.focus_set()
00686         self.currentxy = x, y
00687         self.cornerxy = None
00688         gridcell = self.gridcells.get(self.currentxy)
00689         if gridcell is not None:
00690             gridcell['bg'] = 'yellow'

Here is the call graph for this function:

Here is the caller graph for this function:

def ss1.SheetGUI.shift_return_event (   self,
  event 
)

Definition at line 750 of file ss1.py.

00750 
00751     def shift_return_event(self, event):
00752         "Callback for the Return key with Shift modifier."
00753         self.change_cell()
00754         x, y = self.currentxy
00755         self.setcurrent(x, max(1, y-1))
00756         return "break"

Here is the call graph for this function:

def ss1.SheetGUI.shift_tab_event (   self,
  event 
)

Definition at line 764 of file ss1.py.

00764 
00765     def shift_tab_event(self, event):
00766         "Callback for the Tab key with Shift modifier."
00767         self.change_cell()
00768         x, y = self.currentxy
00769         self.setcurrent(max(1, x-1), y)
00770         return "break"

Here is the call graph for this function:

def ss1.SheetGUI.sync (   self)

Definition at line 795 of file ss1.py.

00795 
00796     def sync(self):
00797         "Fill the GUI cells from the sheet cells."
00798         self.sheet.recalc()
00799         for (x, y), gridcell in self.gridcells.items():
00800             if x == 0 or y == 0:
00801                 continue
00802             cell = self.sheet.getcell(x, y)
00803             if cell is None:
00804                 gridcell['text'] = ""
00805             else:
00806                 if hasattr(cell, 'format'):
00807                     text, alignment = cell.format()
00808                 else:
00809                     text, alignment = str(cell), LEFT
00810                 gridcell['text'] = text
00811                 gridcell['anchor'] = align2anchor[alignment]
00812 

Here is the caller graph for this function:

def ss1.SheetGUI.tab_event (   self,
  event 
)

Definition at line 757 of file ss1.py.

00757 
00758     def tab_event(self, event):
00759         "Callback for the Tab key."
00760         self.change_cell()
00761         x, y = self.currentxy
00762         self.setcurrent(x+1, y)
00763         return "break"

Here is the call graph for this function:

def ss1.SheetGUI.whichxy (   self,
  event 
)

Definition at line 665 of file ss1.py.

00665 
00666     def whichxy(self, event):
00667         w = self.cellgrid.winfo_containing(event.x_root, event.y_root)
00668         if w is not None and isinstance(w, Tk.Label):
00669             try:
00670                 return w.__x, w.__y
00671             except AttributeError:
00672                 pass
00673         return 0, 0

Here is the caller graph for this function:


Member Data Documentation

Definition at line 525 of file ss1.py.

Definition at line 530 of file ss1.py.

Definition at line 583 of file ss1.py.

Definition at line 547 of file ss1.py.

Definition at line 546 of file ss1.py.

Definition at line 527 of file ss1.py.

Definition at line 514 of file ss1.py.

Definition at line 584 of file ss1.py.

Definition at line 663 of file ss1.py.

Definition at line 523 of file ss1.py.

Definition at line 582 of file ss1.py.

Definition at line 528 of file ss1.py.

Definition at line 515 of file ss1.py.


The documentation for this class was generated from the following file: