Spaces:
Runtime error
Runtime error
Update app.py
Browse files359 unmodified lines360361362363364365366150 unmodified lines5175185195205215225235245255 unmodified lines531532533534535536128 unmodified lines665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711359 unmodified linesreturn Truereturn Falsedef find_nearest_atom(self, x, y, threshold=20):"""在分子中找到距离点击位置最近的原子"""if not self.atoms:return None150 unmodified linesrdDepictor.Compute2DCoords(temp_mol)# 绘制分子img = Draw.MolToImage(temp_mol, size=(width, height),highlightAtoms=highlight_atoms,highlightColor=(1, 0, 0))# 添加模式指示器draw = ImageDraw.Draw(img)5 unmodified linesmode_text = f"Mode: {self.mode.capitalize()}"draw.text((10, 10), mode_text, fill="blue", font=font)return img# ==================== Gradio界面 ====================128 unmodified lines"""处理画布点击事件"""x, y = evt.index[0], evt.index[1]# 查找最近的原子nearest_atom = editor.find_nearest_atom(x, y)# 选择模式:只处理选择原子if editor.mode == "select":if nearest_atom is not None:# 设置当前选中的原子editor.selected_atom = nearest_atomeditor.last_clicked_atom = nearest_atomelse:# 点击空白处,取消选择editor.selected_atom = Noneeditor.last_clicked_atom = Nonereturn x, y, editor, update_display(editor)# 绘制模式:处理添加原子和化学键if nearest_atom is not None:# 如果有上次点击的原子,并且不是同一个原子,则添加键if editor.last_clicked_atom is not None and editor.last_clicked_atom != nearest_atom:bond_type = {"SINGLE": BondType.SINGLE,"DOUBLE": BondType.DOUBLE,"TRIPLE": BondType.TRIPLE}.get(bond_type_val, BondType.SINGLE)editor.add_bond(editor.last_clicked_atom, nearest_atom, bond_type)editor.last_clicked_atom = Noneeditor.selected_atom = Noneelse:# 设置当前选中的原子editor.selected_atom = nearest_atomeditor.last_clicked_atom = nearest_atomelse:# 没有点击到原子,添加新原子new_atom = editor.add_atom(x, y, atom_type_val)editor.selected_atom = new_atomeditor.last_clicked_atom = new_atomreturn x, y, editor, update_display(editor)# 事件绑定molecule_display.select(handle_canvas_click,359 unmodified lines360361362363364365366150 unmodified lines5175185195205215225235245255265275285295 unmodified lines535536537538539540541542543544545546547548549550551128 unmodified lines680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725359 unmodified linesreturn Truereturn Falsedef find_nearest_atom(self, x, y, threshold=40):"""在分子中找到距离点击位置最近的原子"""if not self.atoms:return None150 unmodified linesrdDepictor.Compute2DCoords(temp_mol)# 绘制分子try:img = Draw.MolToImage(temp_mol, size=(width, height),highlightAtoms=highlight_atoms,highlightColor=(1, 0, 0))except:# 如果高亮失败,使用默认渲染img = Draw.MolToImage(temp_mol, size=(width, height))# 添加模式指示器draw = ImageDraw.Draw(img)5 unmodified linesmode_text = f"Mode: {self.mode.capitalize()}"draw.text((10, 10), mode_text, fill="blue", font=font)# 如果选择模式下有选中的原子,额外绘制红色边框if self.mode == "select" and self.selected_atom is not None:if self.selected_atom in self.atoms:x, y, _ = self.atoms[self.selected_atom]# 绘制红色圆圈表示选中draw.ellipse([(x-25, y-25), (x+25, y+25)],outline="red",width=3)return img# ==================== Gradio界面 ====================128 unmodified lines"""处理画布点击事件"""x, y = evt.index[0], evt.index[1]# 查找最近的原子 - 使用更灵敏的检测nearest_atom = editor.find_nearest_atom(x, y, threshold=40)# 选择模式:只处理选择原子if editor.mode == "select":if nearest_atom is not None:# 设置当前选中的原子editor.selected_atom = nearest_atom# 在选择模式下,不设置 last_clicked_atomelse:# 点击空白处,取消选择editor.selected_atom = Nonereturn x, y, editor, update_display(editor)# 绘制模式:处理添加原子和化学键if nearest_atom is not None:# 如果有上次点击的原子,并且不是同一个原子,则添加键if editor.last_clicked_atom is not None and editor.last_clicked_atom != nearest_atom:bond_type = {"SINGLE": BondType.SINGLE,"DOUBLE": BondType.DOUBLE,"TRIPLE": BondType.TRIPLE}.get(bond_type_val, BondType.SINGLE)editor.add_bond(editor.last_clicked_atom, nearest_atom, bond_type)editor.last_clicked_atom = Noneeditor.selected_atom = Noneelse:# 设置当前选中的原子editor.selected_atom = nearest_atomeditor.last_clicked_atom = nearest_atomelse:# 没有点击到原子,添加新原子new_atom = editor.add_atom(x, y, atom_type_val)editor.selected_atom = new_atomeditor.last_clicked_atom = new_atomreturn x, y, editor, update_display(editor)# 事件绑定molecule_display.select(handle_canvas_click,
app.py+54-40