@@ -401,7 +401,10 @@ def reset(self):
401401 def recalc (self , rexec ):
402402 if self .value is None :
403403 try :
404- self .value = rexec .r_eval (self .translated )
404+ # A hack to evaluate expressions using true division
405+ rexec .r_exec ("from __future__ import division\n " +
406+ "__value__ = eval(%s)" % repr (self .translated ))
407+ self .value = rexec .r_eval ("__value__" )
405408 except :
406409 exc = sys .exc_info ()[0 ]
407410 if hasattr (exc , "__name__" ):
@@ -490,7 +493,6 @@ class SheetGUI:
490493
491494 TO DO:
492495 - clear multiple cells
493- - Select rows or columns
494496 - Insert, clear, remove rows or columns
495497 - Show new contents while typing
496498 - Scroll bars
@@ -535,6 +537,7 @@ def __init__(self, filename="sheet1.xml", rows=10, columns=5):
535537 self .entry .bind ("<Shift-Return>" , self .shift_return_event )
536538 self .entry .bind ("<Tab>" , self .tab_event )
537539 self .entry .bind ("<Shift-Tab>" , self .shift_tab_event )
540+ self .entry .bind ("<Delete>" , self .delete_event )
538541 # Now create the cell grid
539542 self .makegrid (rows , columns )
540543 # Select the top-left cell
@@ -544,36 +547,103 @@ def __init__(self, filename="sheet1.xml", rows=10, columns=5):
544547 # Copy the sheet cells to the GUI cells
545548 self .sync ()
546549
550+ def delete_event (self , event ):
551+ if self .cornerxy != self .currentxy and self .cornerxy is not None :
552+ self .sheet .clearcells (* (self .currentxy + self .cornerxy ))
553+ else :
554+ self .sheet .clearcell (* self .currentxy )
555+ self .sync ()
556+ self .entry .delete (0 , 'end' )
557+ return "break"
558+
547559 def makegrid (self , rows , columns ):
548560 """Helper to create the grid of GUI cells.
549561
550562 The edge (x==0 or y==0) is filled with labels; the rest is real cells.
551563 """
564+ self .rows = rows
565+ self .columns = columns
552566 self .gridcells = {}
553567 # Create the top row of labels
554568 for x in range (1 , columns + 1 ):
555569 self .cellgrid .grid_columnconfigure (x , minsize = 64 )
556570 cell = Tk .Label (self .cellgrid , text = colnum2name (x ), relief = 'raised' )
557571 cell .grid_configure (column = x , row = 0 , sticky = 'WE' )
558572 self .gridcells [x , 0 ] = cell
573+ cell .__x = x
574+ cell .__y = 0
575+ cell .bind ("<ButtonPress-1>" , self .selectcolumn )
576+ cell .bind ("<B1-Motion>" , self .extendcolumn )
577+ cell .bind ("<ButtonRelease-1>" , self .extendcolumn )
578+ cell .bind ("<Shift-Button-1>" , self .extendcolumn )
559579 # Create the leftmost column of labels
560580 for y in range (1 , rows + 1 ):
561581 cell = Tk .Label (self .cellgrid , text = str (y ), relief = 'raised' )
562582 cell .grid_configure (column = 0 , row = y , sticky = 'WE' )
563583 self .gridcells [0 , y ] = cell
584+ cell .__x = 0
585+ cell .__y = y
586+ cell .bind ("<ButtonPress-1>" , self .selectrow )
587+ cell .bind ("<B1-Motion>" , self .extendrow )
588+ cell .bind ("<ButtonRelease-1>" , self .extendrow )
589+ cell .bind ("<Shift-Button-1>" , self .extendrow )
564590 # Create the real cells
565591 for x in range (1 , columns + 1 ):
566592 for y in range (1 , rows + 1 ):
567593 cell = Tk .Label (self .cellgrid , relief = 'sunken' ,
568594 bg = 'white' , fg = 'black' )
569595 cell .grid_configure (column = x , row = y , sticky = 'NWSE' )
570596 self .gridcells [x , y ] = cell
571- def helper (event , self = self , x = x , y = y ):
572- self .setcurrent (x , y )
573- cell .bind ("<Button-1>" , helper )
574- def shelper (event , self = self , x = x , y = y ):
575- self .setcorner (x , y )
576- cell .bind ("<Shift-Button-1>" , shelper )
597+ cell .__x = x
598+ cell .__y = y
599+ # Bind mouse events
600+ cell .bind ("<ButtonPress-1>" , self .press )
601+ cell .bind ("<B1-Motion>" , self .motion )
602+ cell .bind ("<ButtonRelease-1>" , self .release )
603+ cell .bind ("<Shift-Button-1>" , self .release )
604+
605+ def selectcolumn (self , event ):
606+ x , y = self .whichxy (event )
607+ self .setcurrent (x , 1 )
608+ self .setcorner (x , self .rows )
609+
610+ def extendcolumn (self , event ):
611+ x , y = self .whichxy (event )
612+ if x > 0 :
613+ self .setcurrent (self .currentxy [0 ], 1 )
614+ self .setcorner (x , self .rows )
615+
616+ def selectrow (self , event ):
617+ x , y = self .whichxy (event )
618+ self .setcurrent (1 , y )
619+ self .setcorner (self .columns , y )
620+
621+ def extendrow (self , event ):
622+ x , y = self .whichxy (event )
623+ if y > 0 :
624+ self .setcurrent (1 , self .currentxy [1 ])
625+ self .setcorner (self .columns , y )
626+
627+ def press (self , event ):
628+ x , y = self .whichxy (event )
629+ if x > 0 and y > 0 :
630+ self .setcurrent (x , y )
631+
632+ def motion (self , event ):
633+ x , y = self .whichxy (event )
634+ if x > 0 and y > 0 :
635+ self .setcorner (x , y )
636+
637+ release = motion
638+
639+ def whichxy (self , event ):
640+ w = self .cellgrid .winfo_containing (event .x_root , event .y_root )
641+ if w is not None and isinstance (w , Tk .Label ):
642+ try :
643+ return w .__x , w .__y
644+ except AttributeError :
645+ pass
646+ return 0 , 0
577647
578648 def save (self ):
579649 self .sheet .save (self .filename )
@@ -600,7 +670,7 @@ def setcurrent(self, x, y):
600670 self .cornerxy = None
601671 gridcell = self .gridcells .get (self .currentxy )
602672 if gridcell is not None :
603- gridcell ['bg' ] = 'lightBlue '
673+ gridcell ['bg' ] = 'yellow '
604674
605675 def setcorner (self , x , y ):
606676 if self .currentxy is None or self .currentxy == (x , y ):
@@ -619,6 +689,9 @@ def setcorner(self, x, y):
619689 gridcell = self .gridcells .get ((x , y ))
620690 if gridcell is not None :
621691 gridcell ['bg' ] = 'lightBlue'
692+ gridcell = self .gridcells .get (self .currentxy )
693+ if gridcell is not None :
694+ gridcell ['bg' ] = 'yellow'
622695 name1 = cellname (* self .currentxy )
623696 name2 = cellname (* self .cornerxy )
624697 self .beacon ['text' ] = "%s:%s" % (name1 , name2 )
@@ -732,7 +805,11 @@ def test_basic():
732805
733806def test_gui ():
734807 "GUI test."
735- g = SheetGUI ()
808+ if sys .argv [1 :]:
809+ filename = sys .argv [1 ]
810+ else :
811+ filename = "sheet1.xml"
812+ g = SheetGUI (filename )
736813 g .root .mainloop ()
737814
738815if __name__ == '__main__' :
0 commit comments