[Python-checkins] python/dist/src/Demo/tkinter/guido ss1.py,1.1,1.2

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Fri, 01 Nov 2002 22:25:53 -0800


Update of /cvsroot/python/python/dist/src/Demo/tkinter/guido
In directory usw-pr-cvs1:/tmp/cvs-serv14901

Modified Files:
	ss1.py 
Log Message:
Major breakthrough in selection -- drag-select multiple cells now
works.  Also row and column selection works (sort of).  The DEL
key deletes the selected rectangle.  sys.argv[1] used by test_gui().


Index: ss1.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Demo/tkinter/guido/ss1.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** ss1.py	28 Oct 2002 01:06:37 -0000	1.1
--- ss1.py	2 Nov 2002 06:25:51 -0000	1.2
***************
*** 402,406 ****
          if self.value is None:
              try:
!                 self.value = rexec.r_eval(self.translated)
              except:
                  exc = sys.exc_info()[0]
--- 402,409 ----
          if self.value is None:
              try:
!                 # A hack to evaluate expressions using true division
!                 rexec.r_exec("from __future__ import division\n" +
!                              "__value__ = eval(%s)" % repr(self.translated))
!                 self.value = rexec.r_eval("__value__")
              except:
                  exc = sys.exc_info()[0]
***************
*** 491,495 ****
      TO DO:
      - clear multiple cells
-     - Select rows or columns
      - Insert, clear, remove rows or columns
      - Show new contents while typing
--- 494,497 ----
***************
*** 536,539 ****
--- 538,542 ----
          self.entry.bind("<Tab>", self.tab_event)
          self.entry.bind("<Shift-Tab>", self.shift_tab_event)
+         self.entry.bind("<Delete>", self.delete_event)
          # Now create the cell grid
          self.makegrid(rows, columns)
***************
*** 545,548 ****
--- 548,560 ----
          self.sync()
  
+     def delete_event(self, event):
+         if self.cornerxy != self.currentxy and self.cornerxy is not None:
+             self.sheet.clearcells(*(self.currentxy + self.cornerxy))
+         else:
+             self.sheet.clearcell(*self.currentxy)
+         self.sync()
+         self.entry.delete(0, 'end')
+         return "break"
+ 
      def makegrid(self, rows, columns):
          """Helper to create the grid of GUI cells.
***************
*** 550,553 ****
--- 562,567 ----
          The edge (x==0 or y==0) is filled with labels; the rest is real cells.
          """
+         self.rows = rows
+         self.columns = columns
          self.gridcells = {}
          # Create the top row of labels
***************
*** 557,560 ****
--- 571,580 ----
              cell.grid_configure(column=x, row=0, sticky='WE')
              self.gridcells[x, 0] = cell
+             cell.__x = x
+             cell.__y = 0
+             cell.bind("<ButtonPress-1>", self.selectcolumn)
+             cell.bind("<B1-Motion>", self.extendcolumn)
+             cell.bind("<ButtonRelease-1>", self.extendcolumn)
+             cell.bind("<Shift-Button-1>", self.extendcolumn)
          # Create the leftmost column of labels
          for y in range(1, rows+1):
***************
*** 562,565 ****
--- 582,591 ----
              cell.grid_configure(column=0, row=y, sticky='WE')
              self.gridcells[0, y] = cell
+             cell.__x = 0
+             cell.__y = y
+             cell.bind("<ButtonPress-1>", self.selectrow)
+             cell.bind("<B1-Motion>", self.extendrow)
+             cell.bind("<ButtonRelease-1>", self.extendrow)
+             cell.bind("<Shift-Button-1>", self.extendrow)
          # Create the real cells
          for x in range(1, columns+1):
***************
*** 569,578 ****
                  cell.grid_configure(column=x, row=y, sticky='NWSE')
                  self.gridcells[x, y] = cell
!                 def helper(event, self=self, x=x, y=y):
!                     self.setcurrent(x, y)
!                 cell.bind("<Button-1>", helper)
!                 def shelper(event, self=self, x=x, y=y):
!                     self.setcorner(x, y)
!                 cell.bind("<Shift-Button-1>", shelper)
  
      def save(self):
--- 595,648 ----
                  cell.grid_configure(column=x, row=y, sticky='NWSE')
                  self.gridcells[x, y] = cell
!                 cell.__x = x
!                 cell.__y = y
!                 # Bind mouse events
!                 cell.bind("<ButtonPress-1>", self.press)
!                 cell.bind("<B1-Motion>", self.motion)
!                 cell.bind("<ButtonRelease-1>", self.release)
!                 cell.bind("<Shift-Button-1>", self.release)
! 
!     def selectcolumn(self, event):
!         x, y = self.whichxy(event)
!         self.setcurrent(x, 1)
!         self.setcorner(x, self.rows)
! 
!     def extendcolumn(self, event):
!         x, y = self.whichxy(event)
!         if x > 0:
!             self.setcurrent(self.currentxy[0], 1)
!             self.setcorner(x, self.rows)
! 
!     def selectrow(self, event):
!         x, y = self.whichxy(event)
!         self.setcurrent(1, y)
!         self.setcorner(self.columns, y)
! 
!     def extendrow(self, event):
!         x, y = self.whichxy(event)
!         if y > 0:
!             self.setcurrent(1, self.currentxy[1])
!             self.setcorner(self.columns, y)
! 
!     def press(self, event):
!         x, y = self.whichxy(event)
!         if x > 0 and y > 0:
!             self.setcurrent(x, y)
! 
!     def motion(self, event):
!         x, y = self.whichxy(event)
!         if x > 0 and y > 0:
!             self.setcorner(x, y)
! 
!     release = motion
! 
!     def whichxy(self, event):
!         w = self.cellgrid.winfo_containing(event.x_root, event.y_root)
!         if w is not None and isinstance(w, Tk.Label):
!             try:
!                 return w.__x, w.__y
!             except AttributeError:
!                 pass
!         return 0, 0
  
      def save(self):
***************
*** 601,605 ****
          gridcell = self.gridcells.get(self.currentxy)
          if gridcell is not None:
!             gridcell['bg'] = 'lightBlue'
  
      def setcorner(self, x, y):
--- 671,675 ----
          gridcell = self.gridcells.get(self.currentxy)
          if gridcell is not None:
!             gridcell['bg'] = 'yellow'
  
      def setcorner(self, x, y):
***************
*** 620,623 ****
--- 690,696 ----
                  if gridcell is not None:
                      gridcell['bg'] = 'lightBlue'
+         gridcell = self.gridcells.get(self.currentxy)
+         if gridcell is not None:
+             gridcell['bg'] = 'yellow'
          name1 = cellname(*self.currentxy)
          name2 = cellname(*self.cornerxy)
***************
*** 733,737 ****
  def test_gui():
      "GUI test."
!     g = SheetGUI()
      g.root.mainloop()
  
--- 806,814 ----
  def test_gui():
      "GUI test."
!     if sys.argv[1:]:
!         filename = sys.argv[1]
!     else:
!         filename = "sheet1.xml"
!     g = SheetGUI(filename)
      g.root.mainloop()