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 # create button that lets user select a signal file for this pot when # right-clicked pot_alpha_window = canvas.create_rectangle(ox + (w - POT_ALPHA_WIDTH) / 2, oy + (h - POT_ALPHA_HEIGHT) / 2, ox + (w + POT_ALPHA_WIDTH) / 2, oy + (h + POT_ALPHA_HEIGHT) / 2, fill=POT_ALPHA_FILL if self.signal_file else POT_ALPHA_EMPTY_FILL, outline=POT_ALPHA_OUTLINE) pot_alpha_text = canvas.create_text(ox + w / 2, oy + h / 2 - 1, text=POT_ALPHA_TEXT, justify=CENTER, fill='white' if self.signal_file else 'black', font=FONT) def set_signal_file(): """ Opens a window to let the user choose a signal file. """ new_signal_file = askopenfilename(title=OPEN_POT_SIGNAL_FILE_TITLE, filetypes=[('%s files' % POT_SIGNAL_FILE_TYPE, POT_SIGNAL_FILE_EXTENSION)], initialfile=self.signal_file) if new_signal_file and new_signal_file != self.signal_file: self.signal_file = relpath(new_signal_file) canvas.itemconfig(pot_alpha_window, fill=POT_ALPHA_FILL) canvas.itemconfig(pot_alpha_text, fill='white') self.on_signal_file_changed() self.set_signal_file = set_signal_file self.parts.add(pot_alpha_window) self.parts.add(pot_alpha_text)
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