PYTHON   9

LifeWidgets.py

Guest on 6th June 2021 05:08:04 PM

  1. #   Widgets for Game of Life display
  2.  
  3. from graphics import *
  4.  
  5. class Button:
  6.     #   A "Button" is a labeled rectangle in a window. It
  7.     #   may be "active" or "inactive". The clicked method
  8.     #   returns true if the button is active and the point
  9.     #   specified by the argument is inside the button.
  10.  
  11.     #   Coordinates are specified in window coordinates. The
  12.     #   client is responsible for adjusting the size of the
  13.     #   text to match the size of the button
  14.  
  15.     def __init__(self, win, center, width, height,
  16.                  activeText, inactiveText = None,
  17.                  activeColor='red', inactiveColor = 'darkgray',
  18.                  textColor = 'lightyellow'):
  19.         w, h = abs(width/2.0), abs(height/2.0)
  20.         x, y = center.getX(), center.getY()
  21.         self.__xMax, self.__xMin = x+w, x-w
  22.         self.__yMax, self.__yMin = y+h, y-h
  23.         p1 = Point(self.__xMin, self.__yMin)
  24.         p2 = Point(self.__xMax, self.__yMax)
  25.         self.__rect = Rectangle(p1, p2)
  26.         self.__rect.setFill(activeColor)
  27.         self.__rect.draw(win)
  28.         self.__label = Text(center, activeText)
  29.         self.__label.setTextColor(textColor)
  30.         self.__label.draw(win)
  31.         self.__activeColor = activeColor
  32.         self.__inactiveColor = inactiveColor
  33.         self.__activeText = activeText
  34.         self.__inactiveText = inactiveText
  35.         self.__active = True
  36.         self.activate()
  37.         return
  38.  
  39.     #   The following method tests the button to see if it
  40.     #   was click. p is a Point object. Returns true if the
  41.     #   button is active and also if p is a valid Point
  42.     #   inside the button
  43.     def clicked(self, p):
  44.         return (p is not None and self.__active and
  45.                 self.__xMin <= p.getX() < self.__xMax and
  46.                 self.__yMin <= p.getY() < self.__yMax)
  47.            
  48.     def activate(self):
  49.         if not self.__active:
  50.             self.__active = True
  51.             self.__rect.setFill(self.__activeColor)
  52.             self.__label.setText(self.__activeText)
  53.         return
  54.  
  55.     def deactivate(self):
  56.         if self.__active:
  57.             self.__active = False
  58.             self.__rect.setFill(self.__inactiveColor)
  59.             if self.__inactiveText is not None:
  60.                 self.__label.setText(self.__inactiveText)
  61.         return
  62.  
  63. class InfoBox:
  64.     #   This class is patterned after the text information
  65.     #   exercises in the textbook
  66.    
  67.     def __init__(self, win, center, labelWidth, infoWidth,
  68.                  height, label, infoValue = 0,
  69.                  boxType = 'infoOnly'):
  70.         wl, wih = abs(labelWidth/2), abs(infoWidth/2)
  71.         h = abs(height/2)
  72.         cx, cy = center.getX(), center.getY()
  73.         cx_label = cx - infoWidth/2
  74.         cx_info  = cx + labelWidth/2
  75.        
  76.         p_label  = Point(cx_label, cy)
  77.         p_info   = Point(cx_info, cy)
  78.  
  79.         self.__label = Text(p_label, label)
  80.         self.__label.draw(win)
  81.  
  82.         self.__boxType = boxType
  83.         if boxType == 'infoOnly':
  84.             self.__info  = Text(p_info, infoValue)
  85.             self.__info.draw(win)
  86.         else:
  87.             self.__input = Entry(p_info, infoValue)
  88.             #self.__input.setOutline('black')
  89.             self.__input.setFill('white')
  90.             self.__input.draw(win)
  91.         return
  92.  
  93.     def updateInfo(self, newInfo):
  94.         self.__info.setText(newInfo)
  95.         return
  96.  
  97.     def getInput(self):
  98.         if self.__boxType == 'infoOnly':
  99.             raise Exception('Cannot get input from an ' + \
  100.                             'information-only text box!')
  101.                            
  102.         else:
  103.             return self.__input.getText()

Raw Paste


Login or Register to edit or fork this paste. It's free.