def move(self, dx, dy): self.undraw() self.line.move(dx, dy) if self.text is not None: self.text.move(dx, dy) self.pointer.move(dx, dy) self.p1 = Point(self.p1.x + dx, self.p1.y + dy) self.p2 = Point(self.p2.x + dx, self.p2.y + dy) self.draw(self.graphwin)
def CosineCurve(t, control_points): if not len(control_points) > 2: raise GraphicsError("Length of Control Points must be greater than 2") total_distance = control_points[-1].x - control_points[0].x time_x = t * total_distance + control_points[0].x for i, point in enumerate(control_points): if time_x < point.x: lower_point = i - 1 break upper_point = lower_point + 1 points_distance = control_points[lower_point].distance_x( control_points[upper_point]) x = (1 - t) * control_points[lower_point].x + t * control_points[upper_point].x proportion_done = (time_x - control_points[lower_point].x) / points_distance ft = t * math.pi f = (1 - math.cos(ft)) / 2 return Point(x, lower_point * (1 - f) + upper_point * f)
def draw_month(): window.destroy_all_radiobuttons() for text in other_dates: text.destroy() for week in range(6): for day in range(7): date = day + week * 7 in_month = True if date <= first_day: date = previous_number_of_days - abs(date - first_day) in_month = False else: date -= first_day if date > number_of_days: date -= number_of_days in_month = False if in_month: checkboxes.append(Checkbox(Button(Rectangle(Point(20 + day * 60, 125 + 50 * week), Point(70 + day * 60, 175 + 50 * week), fill=ColourRGB(255, 83, 84), outline_width=0), label=Text(Point(45 + day * 60, 150 + 50 * week), date, font_size=15, font_face="century gothic", font_colour=WHITE)), Button(Rectangle(Point(20 + day * 60, 125 + 50 * week), Point(70 + day * 60, 175 + 50 * week), fill=WHITE, outline_width=0), Rectangle(Point(20 + day * 60, 125 + 50 * week), Point(70 + day * 60, 175 + 50 * week), fill=LIGHTEST_PINK, outline_width=0), label=Text(Point(45 + day * 60, 150 + 50 * week), date, font_size=15, font_face="century gothic", font_colour=DARKER_GREY)), state=False)) else: other_dates.append(Text(Point(45 + day * 60, 150 + 50 * week), date, font_size=15, font_face="century gothic", font_colour=LIGHTER_GREY).draw(window)) date_buttons = RadioButton(*checkboxes).draw(window)
def LinearCurve(t, control_points): if not len(control_points) > 2: raise GraphicsError("Length of Control Points must be greater than 2") total_distance = control_points[-1].x - control_points[0].x time_x = t * total_distance + control_points[0].x for i, point in enumerate(control_points): if time_x < point.x: lower_point = i - 1 break upper_point = lower_point + 1 points_distance = control_points[lower_point].distance_x( control_points[upper_point]) x = (1 - t) * control_points[lower_point].x + t * control_points[upper_point].x proportion_done = (time_x - control_points[lower_point].x) / points_distance return Point(x, (1 - proportion_done) * control_points[lower_point].y + proportion_done * control_points[upper_point].y)
def date_selector(): # Creating a Window on which the calendar is drawn with a width of 450 and height of 480. # The remove_title_bar parameter (default False) removes the title bar from the top of the window # The window bounds (default whole window) define what counts as clicking the window (for dragging) # Here, the bounds are set to a Rectangle object, the other option is an Image object. # We set the window to be draggable, if the user clicks inside the window's bounds, the window will be dragged window = Window(title="goopylib Calendar Example", width=450, height=480, remove_title_bar=True, bounds=Rectangle(Point(100, 0), Point(350, 60))).set_draggable() Image(Point(225, 240), "CalendarBackground.png").draw(window) # Drawing the Background Image current_year = int(datetime.datetime.now().strftime('%Y')) # Gets the current year current_month = datetime.datetime.now().strftime('%B') # Gets the name of the current month current_month_index = int(datetime.datetime.now().strftime('%m')) # Gets the index of the current month (Jan is 1) # first_day is an integer (Monday is 1, Sunday is 7) which defines what day of the week the first date of the # month fell on # number_of_dates is the number of day in this month first_day, number_of_days = calendar.monthrange(current_year, current_month_index) # these are the number of days in the previous month previous_number_of_days = calendar.monthrange(current_year, current_month_index - 1)[1] # This is a button to accept the date selected. It is made from 2 image objects, the first one is the normal state # and the second one is the state when you hover over (the same image, enlarged by a factor of 105%) accept_button = Button(Image(Point(380, 448), "AcceptButton.png"), Image(Point(380, 448), "AcceptButton.png").resize_factor(1.05)).draw(window) # This is the title text showing the selected month and year. The font, style, colour, and size are all given too. title = Text(Point(225, 31), f"{current_month} {current_year}", font_face="century gothic", font_style="bold", font_colour=WHITE, font_size=25).draw(window) # This is the button to close the window given next to the title. close_button = Button(Image(Point(73, 33), "CrossButton.png")).draw(window) # These are buttons to navigate the months. Pressing left takes you to the previous month and right to the next left_arrow = Image(Point(35, 33), "Arrow.png").draw(window) right_arrow = Image(Point(415, 33), "Arrow.png").flip_x().draw(window) checkboxes = [] other_dates = [] def draw_month(): window.destroy_all_radiobuttons() for text in other_dates: text.destroy() for week in range(6): for day in range(7): date = day + week * 7 in_month = True if date <= first_day: date = previous_number_of_days - abs(date - first_day) in_month = False else: date -= first_day if date > number_of_days: date -= number_of_days in_month = False if in_month: checkboxes.append(Checkbox(Button(Rectangle(Point(20 + day * 60, 125 + 50 * week), Point(70 + day * 60, 175 + 50 * week), fill=ColourRGB(255, 83, 84), outline_width=0), label=Text(Point(45 + day * 60, 150 + 50 * week), date, font_size=15, font_face="century gothic", font_colour=WHITE)), Button(Rectangle(Point(20 + day * 60, 125 + 50 * week), Point(70 + day * 60, 175 + 50 * week), fill=WHITE, outline_width=0), Rectangle(Point(20 + day * 60, 125 + 50 * week), Point(70 + day * 60, 175 + 50 * week), fill=LIGHTEST_PINK, outline_width=0), label=Text(Point(45 + day * 60, 150 + 50 * week), date, font_size=15, font_face="century gothic", font_colour=DARKER_GREY)), state=False)) else: other_dates.append(Text(Point(45 + day * 60, 150 + 50 * week), date, font_size=15, font_face="century gothic", font_colour=LIGHTER_GREY).draw(window)) date_buttons = RadioButton(*checkboxes).draw(window) for week in range(6): for day in range(7): date = day + week * 7 in_month = True if date <= first_day: date = previous_number_of_days - abs(date - first_day) in_month = False else: date -= first_day if date > number_of_days: date -= number_of_days in_month = False if in_month: checkboxes.append(Checkbox(Button(Rectangle(Point(20 + day * 60, 125 + 50 * week), Point(70 + day * 60, 175 + 50 * week), fill=ColourRGB(255, 83, 84), outline_width=0), label=Text(Point(45 + day * 60, 150 + 50 * week), date, font_size=15, font_face="century gothic", font_colour=WHITE)), Button(Rectangle(Point(20 + day * 60, 125 + 50 * week), Point(70 + day * 60, 175 + 50 * week), fill=BLACK, outline_width=0), Rectangle(Point(20 + day * 60, 125 + 50 * week), Point(70 + day * 60, 175 + 50 * week), fill=LIGHTEST_PINK, outline_width=0), label=Text(Point(45 + day * 60, 150 + 50 * week), date, font_size=15, font_face="century gothic", font_colour=DARKER_GREY)), state=False)) else: other_dates.append(Text(Point(45 + day * 60, 150 + 50 * week), date, font_size=15, font_face="century gothic", font_colour=LIGHTER_GREY).draw(window)) date_buttons = RadioButton(*checkboxes).draw(window) while window.is_open(): key = window.check_key_press() mouse_pos = window.check_left_mouse_click() if key == "Escape" or close_button.is_clicked(mouse_pos) \ or accept_button.is_clicked(mouse_pos) or key == "Return": window.close() elif left_arrow.is_clicked(mouse_pos): draw_month() elif right_arrow.is_clicked(mouse_pos): pass window.update() """
def __init__(self, p, length, orientation, minimum, maximum, value, pointer, step=1, style=None, outline=None, width=None, text=None, label="Value "): self.range = [minimum - 1, maximum + 1] self.state = value - minimum if not self.range[1] > value > self.range[0]: raise GraphicsError( f"\n\nSlide Bar value must between min/max values: {minimum} < value ({value}) < {maximum}" ) self.anchor = p.clone() self.maximum = maximum self.minimum = minimum if self.minimum > self.maximum: self.minimum, self.maximum = self.maximum, self.minimum elif self.minimum == self.maximum: raise GraphicsError( "\n\nMinimum and Maximum value cannot be the same") if orientation == "vertical": self.p1 = Point(self.anchor.x, self.anchor.y - length / 2) self.p2 = Point(self.anchor.x, self.anchor.y + length / 2) self.scale = self.p1.distance_y( self.p2) / (self.maximum - self.minimum) self.value_points = [ y * self.scale + self.p1.y for y in range(int((maximum - minimum + 1) / step)) ] else: self.p1 = Point(self.anchor.x - length / 2, self.anchor.y) self.p2 = Point(self.anchor.x + length / 2, self.anchor.y) self.scale = self.p1.distance_x( self.p2) / (self.maximum - self.minimum) self.value_points = [ x * self.scale + self.p1.x for x in range(int((maximum - minimum + 1) / step)) ] self.graphwin = None self.drawn = False self.selected = False self.orientation = orientation self.length = length self.step = step self.pointer = pointer self.text = text self.line = None self.style = style self.outline = outline self.width = width self.style = style self.label = label self.set_value(value) self.reload() GraphicsObject.slider_instances.add(self)
class Slider: def __init__(self, p, length, orientation, minimum, maximum, value, pointer, step=1, style=None, outline=None, width=None, text=None, label="Value "): self.range = [minimum - 1, maximum + 1] self.state = value - minimum if not self.range[1] > value > self.range[0]: raise GraphicsError( f"\n\nSlide Bar value must between min/max values: {minimum} < value ({value}) < {maximum}" ) self.anchor = p.clone() self.maximum = maximum self.minimum = minimum if self.minimum > self.maximum: self.minimum, self.maximum = self.maximum, self.minimum elif self.minimum == self.maximum: raise GraphicsError( "\n\nMinimum and Maximum value cannot be the same") if orientation == "vertical": self.p1 = Point(self.anchor.x, self.anchor.y - length / 2) self.p2 = Point(self.anchor.x, self.anchor.y + length / 2) self.scale = self.p1.distance_y( self.p2) / (self.maximum - self.minimum) self.value_points = [ y * self.scale + self.p1.y for y in range(int((maximum - minimum + 1) / step)) ] else: self.p1 = Point(self.anchor.x - length / 2, self.anchor.y) self.p2 = Point(self.anchor.x + length / 2, self.anchor.y) self.scale = self.p1.distance_x( self.p2) / (self.maximum - self.minimum) self.value_points = [ x * self.scale + self.p1.x for x in range(int((maximum - minimum + 1) / step)) ] self.graphwin = None self.drawn = False self.selected = False self.orientation = orientation self.length = length self.step = step self.pointer = pointer self.text = text self.line = None self.style = style self.outline = outline self.width = width self.style = style self.label = label self.set_value(value) self.reload() GraphicsObject.slider_instances.add(self) def __repr__(self): return "SlideBar({}, {}, {}-{})".format(self.p1, self.p2, self.minimum, self.maximum) def set_selected(self, selected=True): self.selected = selected def draw(self, canvas): self.graphwin = canvas if self.text is not None: self.text.draw(canvas) self.line.draw(canvas) self.pointer.draw(canvas) self.drawn = True return self def get_anchor(self): return self.anchor def reload(self): self.line = Line(self.p1, self.p2, style=self.style, outline=self.outline, outline_width=self.width) if self.text is not None: self.text.set_text(f"{self.label}{self.state}") try: selected_clicks = self.pointer.selected_clicks except AttributeError: selected_clicks = -1 if self.orientation == "vertical": self.pointer.move_to(self.anchor.x, ((self.state - self.minimum) * self.scale) + self.p1.y) else: self.pointer.move_to( ((self.state - self.minimum) * self.scale) + self.p1.x, self.anchor.y) self.pointer.selected_clicks = selected_clicks def undraw(self): self.line.undraw() if self.text is not None: self.text.undraw() self.pointer.undraw() self.drawn = False def redraw(self): self.undraw() self.reload() self.draw(self.graphwin) def set_value(self, value): if value < self.minimum or value > self.maximum: raise GraphicsError( f"\n\nValue to set the Slider Bar must be within {self.minimum} and {self.maximum} not {value}" ) self.state = value try: self.redraw() except AttributeError: self.reload() def change_value(self, value): self.set_value(self.state + value) def mouse_value(self, mouse_pos): self.set_value(self.minimum + self.value_points.index( min(self.value_points, key=lambda x: abs(x - mouse_pos))) * self.step) def move(self, dx, dy): self.undraw() self.line.move(dx, dy) if self.text is not None: self.text.move(dx, dy) self.pointer.move(dx, dy) self.p1 = Point(self.p1.x + dx, self.p1.y + dy) self.p2 = Point(self.p2.x + dx, self.p2.y + dy) self.draw(self.graphwin) def is_clicked(self, mouse_pos): return self.pointer.is_clicked(mouse_pos) or self.line.is_clicked( mouse_pos) def is_selected(self): return self.pointer.is_selected() def get_value(self): return self.state def bind_to_mouse_wheel(self, state=True): if state: if self not in GraphicsObject.slider_instances_bound: GraphicsObject.slider_instances_bound.add(self) else: GraphicsObject.slider_instances_bound.discard(self)