def set_resistance(r): """ Sets the resistance of this resistor to be the string |r|, after appropriately modifying it. """ if not r: self.board.display_message('No resistance entered', ERROR) return try: old_r = self.get_resistance() new_r = resistance_to_string(resistance_from_string(r)) do = lambda: _set_resistance(new_r) undo = lambda: _set_resistance(old_r) do() self.board.set_changed(True, Action(do, undo, 'set_resistance')) except Exception as e: self.board.display_message(e.message, ERROR)
def draw_on(self, canvas, offset=(0, 0)): ox, oy = offset w, h = self.width, self.height self._resistor_zig_zags = draw_resistor_zig_zags(canvas, ox, oy, w, h) self.parts |= self._resistor_zig_zags text = resistance_to_string(resistance_from_string(self.init_resistance)) if w > h: # horizontal self.resistor_text = canvas.create_text(ox + w / 2, oy - RESISTOR_TEXT_PADDING, text=text, font=FONT) else: # vertical self.resistor_text = canvas.create_text(ox + w + RESISTOR_TEXT_PADDING + 8, oy + h / 2, text=text, font=FONT) self.parts.add(self.resistor_text) def get_resistance(): """ Returns the string representing this resistor's resistance. """ return canvas.itemcget(self.resistor_text, 'text') self.get_resistance = get_resistance def _set_resistance(r): canvas.itemconfig(self.resistor_text, text=r) self.init_resistance = r def set_resistance(r): """ Sets the resistance of this resistor to be the string |r|, after appropriately modifying it. """ if not r: self.board.display_message('No resistance entered', ERROR) return try: old_r = self.get_resistance() new_r = resistance_to_string(resistance_from_string(r)) do = lambda: _set_resistance(new_r) undo = lambda: _set_resistance(old_r) do() self.board.set_changed(True, Action(do, undo, 'set_resistance')) except Exception as e: self.board.display_message(e.message, ERROR) self.set_resistance = set_resistance