def mousedownAction(e): nonlocal rect, x0, y0 x0 = e.getX() y0 = e.getY() rect = GRect(x0, y0, 0, 0) rect.setFilled(True) gw.add(rect)
def __init__(self, row, col, sqSize): GRect.__init__(self, sqSize, sqSize) self.setFilled(True) self.setColor("White") self._row = row self._col = col self._link = None self._rank = 0
def __init__(self, row, col, sqSize): GRect.__init__(self, sqSize, sqSize) self.setFilled(True) self.setColor("White") self._row = row self._col = col self._link = None #added rank variable to determine depth of set self._rank = 0
def createFilledRect(x, y, width, height, fill='black', border=None): rect = GRect(x, y, width, height) rect.setFilled(True) if border is None: rect.setColor(fill) else: rect.setColor(border) rect.setFillColor(fill) return rect
def __init__(self, sq1, sq2, sqSize): """Creates a wall between sq1 and sq2.""" if sq1._row == sq2._row and sq1._col + 1 == sq2._col: GRect.__init__(self, 2, sqSize + 2) elif sq1._row + 1 == sq2._row and sq1._col == sq2._col: GRect.__init__(self, sqSize + 2, 2) else: raise ValueError("Wall does not connect adjacent squares") self._sq1 = sq1 self._sq2 = sq2 self.setFilled(True) self.setColor("LightGray")
def create_filled_rect(x, y, width, height, fill="Black", border=None): """ Creates a GRect filled with the specified fill color. If border is specified, the border appears in that color. """ rect = GRect(x - width / 2, y - height / 2, width, height) rect.setFilled(True) if border is None: rect.setColor(fill) else: rect.setColor(border) rect.setFillColor(fill) return rect
def __init__(self, digit): GCompound.__init__(self) if digit != 0: self.digit = str(digit) else: self.digit = None self.cell = GRect(0, 0, CELL_WIDTH, CELL_WIDTH) self.cell.setColor(CELL_BORDER_COLOR) self.cell.setFillColor(CELL_GOOD_COLOR) self.cell.setFilled(True) self.add(self.cell, 0, 0) self.label = None self.only_a_suggestion = True self.render_label() self.selector = None
def __init__(self): GCompound.__init__(self) bar = GRect(GWINDOW_WIDTH, MENU_BAR_HEIGHT) bar.setFilled(True) bar.setColor(CELL_BORDER_COLOR) bar.setFillColor(MENU_BAR_BGCOLOR) self.add(bar, 0, 0) self.label = GLabel("Y O U D O K U") self.label.setFont(MENU_TITLE_FONT) self.label.setColor(MENU_TITLE_COLOR) self.add(self.label, GWINDOW_WIDTH//2 - self.label.getWidth()//2, MENU_BAR_HEIGHT//2 + self.label.getAscent()//2 - 5)
def createBackground(color): background = GRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT) background.setFilled(True) background.setColor(color) return background
def create_object(): """ Creates the original DVD Logo. Code from here will be moved into the new class that you are creating. """ width = 200 height = 200 obj = GCompound() cube = GRect(0, 0, width, height) cube.set_filled(True) cube.set_color(random_color()) obj.add(cube) dvdtext = GLabel("DVD") dvdtext.set_font("bold 60px 'serif'") obj.add(dvdtext, width / 2 - dvdtext.get_width() / 2, height / 2 - 10) vidtext = GLabel("video") vidtext.set_font("bold 50px 'serif'") vidtext.set_color("white") obj.add(vidtext, width / 2 - vidtext.get_width() / 2, height / 2 + vidtext.get_ascent()) return obj
def createRect(x, y, w, h, color): rect = GRect(x, y, w, h) rect.setFilled(True) rect.setColor(color) return rect
def makeDisplay(alphabetLabels, upCardLabels): decisionIndicators = dict() letterWidth = alphabetLabels[0].getWidth() letterHeight = alphabetLabels[0].getHeight() XMARGIN = 50 offset = (GWINDOW_WIDTH - XMARGIN - (len(alphabetLabels) * letterWidth)) / (len(alphabetLabels) + 1) for (i, label) in enumerate(alphabetLabels): x = XMARGIN + (offset + letterWidth) * i + offset gw.add(label, x, GWINDOW_HEIGHT - LETTER_BASE) YMARGIN = 100 upCardOffset = (GWINDOW_HEIGHT - YMARGIN - (len(upCardLabels) * letterHeight)) / (len(upCardLabels) + 1) for (i, label) in enumerate(upCardLabels): y = YMARGIN + (upCardOffset + letterHeight) * i + upCardOffset gw.add(label, LETTER_BASE, y) for (i, upCardLabel) in enumerate(upCardLabels): y = YMARGIN + (upCardOffset + letterHeight) * i + upCardOffset - letterHeight for (j, totalLabel) in enumerate(alphabetLabels): x = XMARGIN + (offset + letterWidth) * j + offset decisionIndicator = GRect(x, y, letterHeight, letterWidth) decisionIndicator.setFillColor("black") decisionIndicator.setFilled(True) gw.add(decisionIndicator) decisionIndicators[(str(int(totalLabel.getLabel())), str(int(upCardLabel.getLabel())))] = decisionIndicator return decisionIndicators
def pyramid(): gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) for i in range(1, BRICKS_IN_BASE + 1): if i % 2 != 0: n = 0 while i > n: X_1 = ((GWINDOW_WIDTH / 2) - (BRICK_WIDTH / 2)) + ((n / 2) * BRICK_WIDTH) X_2 = ((GWINDOW_WIDTH / 2) - (BRICK_WIDTH / 2)) - ((n / 2) * BRICK_WIDTH) Y = 30 + (i * 10) rect_1 = GRect(X_1, Y, BRICK_WIDTH, BRICK_HEIGHT) rect_2 = GRect(X_2, Y, BRICK_WIDTH, BRICK_HEIGHT) gw.add(rect_1) gw.add(rect_2) n += 2 else: n = 0 while i > n: X_1_1 = ((GWINDOW_WIDTH / 2) - BRICK_WIDTH) + ( (n / 2) * BRICK_WIDTH) X_1_2 = ((GWINDOW_WIDTH / 2) - BRICK_WIDTH) - ( (n / 2) * BRICK_WIDTH) X_2_1 = (GWINDOW_WIDTH / 2) + ((n / 2) * BRICK_WIDTH) X_2_2 = (GWINDOW_WIDTH / 2) - ((n / 2) * BRICK_WIDTH) Y = 30 + (i * 10) rect_1_1 = GRect(X_1_1, Y, BRICK_WIDTH, BRICK_HEIGHT) rect_1_2 = GRect(X_1_2, Y, BRICK_WIDTH, BRICK_HEIGHT) rect_2_1 = GRect(X_2_1, Y, BRICK_WIDTH, BRICK_HEIGHT) rect_2_2 = GRect(X_2_2, Y, BRICK_WIDTH, BRICK_HEIGHT) gw.add(rect_1_1) gw.add(rect_1_2) gw.add(rect_2_1) gw.add(rect_2_2) n += 2
def __init__(self, digit): GCompound.__init__(self) self.digit = str(digit) cell = GRect(0, 0, SUBCELL_WIDTH, SUBCELL_WIDTH) cell.setColor(CELL_BORDER_COLOR) cell.setFillColor(SUBCELL_FILL_COLOR) cell.setFilled(True) self.add(cell, 0, 0) self.label = GLabel(digit) self.label.setFont(SUBCELL_FONT) self.label.setColor(SUBCELL_TEXT_COLOR) self.add(self.label, SUBCELL_WIDTH//2 - self.label.getWidth()//2, SUBCELL_WIDTH//2 + self.label.getAscent()//2 - 3)
def _createBackground(self): frame = GRect(0, 0, self._frameWidth, self._frameHeight) frame.setFilled(True) frame.setColor(FRAME_COLOR) self.add(frame) x1 = self._frameWidth / 2 x0 = x1 - PIECE_WIDTH - COLUMN_SEP x2 = x1 + PIECE_WIDTH + COLUMN_SEP y0 = TOP_MARGIN + PIECE_HEIGHT / 2 y1 = self._frameHeight - BOTTOM_MARGIN - PIECE_HEIGHT / 2 h = CHANNEL_WIDTH / 2 poly = GPolygon() poly.addVertex(x0 - h, y1 + h) poly.addVertex(x0 - h, y0 - h) poly.addVertex(x2 + h, y0 - h) poly.addVertex(x2 + h, y1 + h) poly.addVertex(x2 - h, y1 + h) poly.addVertex(x2 - h, y0 + h) poly.addVertex(x1 + h, y0 + h) poly.addVertex(x1 + h, y1 + h) poly.addVertex(x1 - h, y1 + h) poly.addVertex(x1 - h, y0 + h) poly.addVertex(x0 + h, y0 + h) poly.addVertex(x0 + h, y1 + h) poly.addVertex(x0 - h, y1 + h) poly.setFilled(True) poly.setColor(CHANNEL_COLOR) self.add(poly)
def clickAction(e): gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) x0 = (GWINDOW_WIDTH - N_COL * BOX_SIZE) / 2 y0 = (GWINDOW_HEIGHT - N_ROW * BOX_SIZE) / 2 for row in range(N_ROW): for col in range(N_COL): x = x0 + col * BOX_SIZE y = y0 + row * BOX_SIZE box = GRect(x, y, BOX_SIZE, BOX_SIZE) gw.add(box) gw.addEventListener('click', clickAction)
def makePaddle(): #Create paddle global paddle paddle = GRect((GWINDOW_WIDTH - PADDLE_WIDTH) / 2, PADDLE_Y, PADDLE_WIDTH, PADDLE_HEIGHT) paddle.setFilled(True) paddle.setColor("black") gw.add(paddle)
def __init__(self, color, level, puzzle): """Creates a piece with the indicated color and initial level""" GCompound.__init__(self) self._level = level self._puzzle = puzzle self.setColor(color) frame = GRect(PIECE_WIDTH, PIECE_HEIGHT) frame.setFilled(True) frame.setColor(PIECE_COLOR) self.add(frame, -PIECE_WIDTH / 2, 0) poly = GPolygon() dw = PIECE_WIDTH / puzzle.getNLevels() w0 = (level - 1) * dw w1 = level * dw poly.addVertex(-w0 / 2, 0) poly.addVertex(w0 / 2, 0) poly.addVertex(w1 / 2, PIECE_HEIGHT) poly.addVertex(-w1 / 2, PIECE_HEIGHT) poly.setFilled(True) poly.setColor(color) self.add(poly) border = GRect(PIECE_WIDTH, PIECE_HEIGHT) border.setColor(BORDER_COLOR) self.add(border, -PIECE_WIDTH / 2, 0)
def DrawBangladeshFlag(): gw = GWindow(WINDOW_WIDTH, WINDOW_HEIGHT) rect = GRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT) rect.setFilled(True) rect.setColor("ForestGreen") gw.add(rect) circ = GOval(200, 100, 200, 200) circ.setFilled(True) circ.setColor("DarkRed") gw.add(circ)
def background(): gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) rect = GRect(-10, -10, 720, 520) rect.setColor("cyan") rect.setFilled(True) gw.add(rect) for i in range(8): arc = GOval(-100, 100 + 30 * i, 900, 500) arc.setFilled(True) arc.setColor(colors[i]) gw.add(arc)
def __init__(self, node): GCompound.__init__(self) self._node = node frame = GRect(ArpanetMonitor.WIDTH, ArpanetMonitor.MAX_NODES * ArpanetMonitor.VSPACE) self.add(frame, 0, ArpanetMonitor.VSPACE) label = GLabel(node.getName()) label.setFont(ArpanetMonitor.FONT) x = ArpanetMonitor.MARGIN y = label.getAscent() self.add(label, x, y) self._label = label self._lines = [] for i in range(ArpanetMonitor.MAX_NODES): y += ArpanetMonitor.VSPACE label = GLabel("") label.setFont(ArpanetMonitor.FONT) self.add(label, x, y) self._lines.append(label) self.update()
def __init__(self, text, fn=None): GCompound.__init__(self) label = GLabel(text) label.set_font(self.BUTTON_FONT) width = max(self.BUTTON_MIN_WIDTH, 2 * self.BUTTON_MARGIN + label.get_width()) frame = GRect(width, self.BUTTON_DEFAULT_HEIGHT) frame.set_filled(True) frame.set_fill_color("White") self.add(frame) self.add(label) self.text = text self.label = label self.frame = frame self.fn = fn self._recenter()
def rainbow(): gw = GWindow(500, 250) rect = GRect(0, 0, 500, 250) rect.setColor('cyan') rect.setFilled(True) gw.add(rect) red = GOval(X, Y, W, H) red.setColor('Red') red.setFilled(True) orange = GOval(X, 2 * Y, W, H) orange.setColor('Orange') orange.setFilled(True) yellow = GOval(X, 3 * Y, W, H) yellow.setColor('Yellow') yellow.setFilled(True) green = GOval(X, 4 * Y, W, H) green.setColor('Green') green.setFilled(True) blue = GOval(X, 5 * Y, W, H) blue.setColor('blue') blue.setFilled(True) indigo = GOval(X, 6 * Y, W, H) indigo.setColor('indigo') indigo.setFilled(True) violet = GOval(X, 7 * Y, W, H) violet.setColor('violet') violet.setFilled(True) end = GOval(X, 8 * Y, W, H) end.setColor('cyan') end.setFilled(True) gw.add(red) gw.add(orange) gw.add(yellow) gw.add(green) gw.add(blue) gw.add(indigo) gw.add(violet) gw.add(end)
def image_shop(): def add_button(label, action): """ Adds a button to the region on the left side of the window label is the text that will be displayed on the button and action is the callback function that will be run when the button is clicked. """ x = BUTTON_MARGIN y = gs.next_button_y button = GButton(label, action) button.set_size(BUTTON_WIDTH, BUTTON_HEIGHT) gw.add(button, x, y) gs.next_button_y += BUTTON_HEIGHT + BUTTON_MARGIN def set_image(image): """ Sets image as the current image after removing the old one. """ if gs.current_image is not None: gw.remove(gs.current_image) gs.current_image = image x = BUTTON_AREA_WIDTH + (IMAGE_AREA_WIDTH - image.get_width()) / 2 y = (gw.get_height() - image.get_height()) / 2 gw.add(image, x, y) def load_button_action(): """Callback function for the Load button""" filename = choose_input_file() if filename != "": set_image(GImage(filename)) def flip_vertical_action(): """Callback function for the Flip Vertical button""" if gs.current_image is not None: set_image(flip_vertical(gs.current_image)) gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) gs = GState() button_area = GRect(0, 0, BUTTON_AREA_WIDTH, GWINDOW_HEIGHT) button_area.set_filled(True) button_area.set_color(BUTTON_BACKGROUND) gw.add(button_area) gs.next_button_y = BUTTON_MARGIN gs.current_image = None add_button("Load", load_button_action) add_button("Flip Vertical", flip_vertical_action)
def __init__(self, letter, perm, inverse): GCompound.__init__(self) rotor = GRect(ROTOR_WIDTH, ROTOR_HEIGHT) rotor.setColor(ROTOR_BGCOLOR) rotor.setFilled(True) self.add(rotor, -ROTOR_WIDTH / 2, -ROTOR_HEIGHT / 2) # create design for rotors self.ch = GLabel(letter) self.ch.setColor(ROTOR_COLOR) self.ch.setFont(ROTOR_FONT) self.add(self.ch, -self.ch.getWidth() / 2, ROTOR_LABEL_DY) self.perm = perm self.inverse = inverse self.offset = 0 self.rotor = rotor
def DrawMaldivesFlag(): sun_radius = WINDOW_HEIGHT//5 gw = GWindow(WINDOW_WIDTH, WINDOW_HEIGHT) gw.add(createBackground("Crimson")) rect = GRect(WINDOW_WIDTH//8, WINDOW_HEIGHT//8, 3*WINDOW_WIDTH//4, 3*WINDOW_HEIGHT//4) rect.setColor("darkgreen") rect.setFilled(True) gw.add(rect) gw.add(createSun("white")) circ=GOval(WINDOW_WIDTH//2 - (sun_radius-20), WINDOW_HEIGHT//2-(sun_radius), sun_radius*2, sun_radius*2) circ.setColor("darkgreen") circ.setFilled(True) gw.add(circ)
def ImageShop(classifier_file): def addButton(label, action): """ Adds a button to the region on the left side of the window """ nonlocal nextButtonY x = BUTTON_MARGIN y = nextButtonY button = GButton(label, action) button.setSize(BUTTON_WIDTH, BUTTON_HEIGHT) gw.add(button, x, y) nextButtonY += BUTTON_HEIGHT + BUTTON_MARGIN def setImage(image): """ Sets image as the current image after removing the old one. """ nonlocal currentImage if currentImage is not None: gw.remove(currentImage) currentImage = image x = BUTTON_AREA_WIDTH + (IMAGE_AREA_WIDTH - image.getWidth() * image.sf) / 2 y = (gw.getHeight() - image.getHeight() * image.sf) / 2 gw.add(image, x, y) def setThermometer(percentage): if percentage > 0.50: showYes() else: showNo() likelihood.setSize(BUTTON_AREA_WIDTH-10, percentage * (GWINDOW_HEIGHT-nextButtonY-5)) def loadButtonAction(): """Callback function for the Load button""" nonlocal currentFile filename = chooseInputFile() currentFile = filename if filename != "": img = GImage(filename) width = len(img.getPixelArray()) height = len(img.getPixelArray()[0]) max_dim = max(width, height) sf = 750 / max_dim if max_dim > 750: img.scale(sf) setImage(img) clearMessage() def flipVerticalAction(): """Callback function for the FlipVertical button""" if currentImage is not None: setImage(flipVertical(currentImage)) def flipHorizontalAction(): """Callback function for the FlipHorizontal button""" if currentImage is not None: setImage(flipHorizontal(currentImage)) def rotateLeftAction(): """Callback function for the RotateLeft button""" if currentImage is not None: setImage(rotateLeft(currentImage)) def rotateRightAction(): """Callback function for the RotateRight button""" if currentImage is not None: setImage(rotateRight(currentImage)) def isZebraAction(): """Callback function for the Is It a Zebra? button""" if currentFile is not None: zebra_prob = classifier(currentFile)['zebra'] setThermometer(zebra_prob) def showYes(): clearMessage() gw.add(yes, BUTTON_AREA_WIDTH//2-30, GWINDOW_HEIGHT-nextButtonY//2-150) def showNo(): clearMessage() gw.add(no, BUTTON_AREA_WIDTH//2-20, GWINDOW_HEIGHT-nextButtonY//2-150) def clearMessage(): gw.remove(yes) gw.remove(no) gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) buttonArea = GRect(0, 0, BUTTON_AREA_WIDTH, GWINDOW_HEIGHT) buttonArea.setFilled(True) buttonArea.setColor(BUTTON_BACKGROUND) gw.add(buttonArea) nextButtonY = BUTTON_MARGIN currentImage = None currentFile = None addButton("Load", loadButtonAction) addButton("Flip Vertical", flipVerticalAction) addButton("Flip Horizontal", flipHorizontalAction) addButton("Rotate Left", rotateLeftAction) addButton("Rotate Right", rotateRightAction) addButton("Is It a Zebra?", isZebraAction) thermometer = GRect(5, nextButtonY, BUTTON_AREA_WIDTH-10, GWINDOW_HEIGHT-nextButtonY-5) thermometer.setFilled(True) thermometer.setColor("red") likelihood = GRect(5, nextButtonY, BUTTON_AREA_WIDTH-10, 0) likelihood.setFilled(True) likelihood.setColor("green") gw.add(thermometer) gw.add(likelihood) yes = GLabel("YES") yes.setColor("white") yes.setFont("bold 36px 'Monaco','Monospaced'") no = GLabel("NO") no.setColor("white") no.setFont("bold 36px 'Monaco','Monospaced'") from cnn import Classifier classifier = Classifier.load(classifier_file)
def ImageShop(): def addButton(label, action): """ Adds a button to the region on the left side of the window """ nonlocal nextButtonY x = BUTTON_MARGIN y = nextButtonY button = GButton(label, action) button.setSize(BUTTON_WIDTH, BUTTON_HEIGHT) gw.add(button, x, y) nextButtonY += BUTTON_HEIGHT + BUTTON_MARGIN def setImage(image): """ Sets image as the current image after removing the old one. """ nonlocal currentImage if currentImage is not None: gw.remove(currentImage) currentImage = image x = BUTTON_AREA_WIDTH + (IMAGE_AREA_WIDTH - image.getWidth()) / 2 y = (gw.getHeight() - image.getHeight()) / 2 gw.add(image, x, y) def loadButtonAction(): """Callback function for the Load button""" filename = chooseInputFile() if filename != "": setImage(GImage(filename)) def flipVerticalAction(): """Callback function for the FlipVertical button""" if currentImage is not None: setImage(flipVertical(currentImage)) def flipHorizontalAction(): """Callback function for the FlipHorizontal button""" if currentImage is not None: setImage(flipHorizontal(currentImage)) def rotateRightAction(): """Callback function for the RotateRight button""" if currentImage is not None: setImage(rotateRight(currentImage)) def rotateLeftAction(): """Callback function for the RotateLeft button""" if currentImage is not None: setImage(rotateLeft(currentImage)) def grayscaleAction(): """Callback function for the grayscale button""" if currentImage is not None: setImage(createGrayscaleImage(currentImage)) def greenScreenAction(): """Callback function for the GreenScreen button""" if currentImage is not None: setImage(greenScreen(currentImage)) def equalizeAction(): """Callback function for the Equalize button""" if currentImage is not None: setImage(equalize(currentImage)) gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) buttonArea = GRect(0, 0, BUTTON_AREA_WIDTH, GWINDOW_HEIGHT) buttonArea.setFilled(True) buttonArea.setColor(BUTTON_BACKGROUND) gw.add(buttonArea) nextButtonY = BUTTON_MARGIN currentImage = None #Add buttons addButton("Load", loadButtonAction) addButton("Flip Vertical", flipVerticalAction) addButton("Flip Horizontal", flipHorizontalAction) addButton("Rotate Right", rotateRightAction) addButton("Rotate Left", rotateLeftAction) addButton("Grayscale", grayscaleAction) addButton("Green Screen", greenScreenAction) addButton("Equalize", equalizeAction)
def createRedCross(w, h): cross = GCompound() rect_1 = GRect(-w / 2, -h / 2, LONG_SIDE, SHORT_SIDE) rect_1.setFilled(True) rect_1.setColor('red') cross.add(rect_1) rect_2 = GRect(-h / 2, -w / 2, SHORT_SIDE, LONG_SIDE) rect_2.setFilled(True) rect_2.setColor('red') cross.add(rect_2) return cross
def __init__(self, x, y, width, height, color="Black"): GRect.__init__(self, x, y, width, height) self.setFilled(True) self.setColor(color)