Best Python code snippet using ATX
view.py
Source:view.py
1import numpy as np2from typing import Callable, Optional3from src.gui.core.button import Button4from src.gui.core.gui import GUI5from src.gui.core.gui_consts import GUIConsts6from src.gui.core.image import Image7from src.gui.core.label import Label8from src.gui.core.widget import Widget9from src.gui.views.view_base import ViewBase10from src.object_detection.objectDetector import Detection11class RobotView(ViewBase):12 def __init__(self, on_forward: Callable[[bool], any], on_backward: Callable[[bool], any],13 on_turn_left: Callable[[bool], any], on_turn_right: Callable[[bool], any]):14 self.__on_forward = on_forward15 self.__on_backward = on_backward16 self.__on_turn_left = on_turn_left17 self.__on_turn_right = on_turn_right18 self.__gui: Optional[GUI] = None19 self.__detection_widgets: list[Widget] = []20 # âââ¶â·21 self.__button_forward = Button(text='Forward', pos=(0, 0), font_size=1,22 on_mouse_down=lambda: self.__on_forward(True),23 on_mouse_up=lambda: self.__on_forward(False))24 self.__button_backward = Button(text='Backward', pos=(0, 0), font_size=1,25 on_mouse_down=lambda: self.__on_backward(True),26 on_mouse_up=lambda: self.__on_backward(False))27 self.__button_turn_left = Button(text='Turn left', pos=(0, 0), font_size=1, background_color=(136, 150, 0),28 on_mouse_down=lambda: self.__on_turn_left(True),29 on_mouse_up=lambda: self.__on_turn_left(False))30 self.__button_turn_right = Button(text='Turn right', pos=(0, 0), font_size=1, background_color=(136, 150, 0),31 on_mouse_down=lambda: self.__on_turn_right(True),32 on_mouse_up=lambda: self.__on_turn_right(False))33 self.__depth_estimation_image = Image(pos=(0, GUI.DEFAULT_SIZE[1]), size=GUI.DEFAULT_SIZE)34 def load(self, gui: GUI):35 self.__gui = gui36 gui.set_size(GUI.DEFAULT_SIZE)37 width, height = GUI.DEFAULT_SIZE38 btn_size = height // 339 self.__button_forward.set_pos((width // 2, btn_size // 2))40 self.__button_backward.set_pos((width // 2, height - btn_size // 2))41 self.__button_turn_left.set_pos((width // 2 - btn_size, height // 2))42 self.__button_turn_right.set_pos((width // 2 + btn_size, height // 2))43 for button in [self.__button_forward, self.__button_backward, self.__button_turn_left,44 self.__button_turn_right]:45 button.set_size((int(btn_size * 1.618), btn_size))46 button.set_border_width(4)47 gui.add_widgets((self.__button_forward,48 self.__button_backward,49 self.__button_turn_left,50 self.__button_turn_right))51 def toggle_fill_buttons(self, fill: bool):52 for button in [self.__button_forward, self.__button_backward, self.__button_turn_left,53 self.__button_turn_right]:54 button.set_fill(fill)55 if self.__gui is not None:56 self.__gui.redraw()57 def toggle_depth_preview(self, show: bool):58 if self.__gui is None:59 return60 if show:61 self.__gui.set_size((GUI.DEFAULT_SIZE[0], GUI.DEFAULT_SIZE[1] * 2))62 self.__gui.add_widgets((self.__depth_estimation_image,))63 else:64 self.__gui.set_size(GUI.DEFAULT_SIZE)65 self.__gui.remove_widgets(self.__depth_estimation_image)66 def set_depth_estimation_image(self, image: np.ndarray):67 self.__depth_estimation_image.set_image(image)68 def set_steering_button_active(self, name: str, is_active: bool):69 button = self.__button_forward if name == 'forward' else self.__button_backward if name == 'backward' else self.__button_turn_left if name == 'left' else self.__button_turn_right if name == 'right' else None70 if button is not None:71 button.set_text_color((132, 199, 129) if is_active else (255, 255, 255))72 if self.__gui is not None:73 self.__gui.redraw()74 def set_detections(self, detections: list[Detection]):75 if self.__gui is None:76 return77 for widget in self.__detection_widgets:78 self.__gui.remove_widgets(widget)79 for detection in detections:80 # Draw bounding_box81 start_point = detection.bounding_box.left, detection.bounding_box.top82 end_point = detection.bounding_box.right, detection.bounding_box.bottom83 center = (int((start_point[0] + end_point[0]) / 2), int((start_point[1] + end_point[1]) / 2))84 color = (64, 64, 255)85 rect = Button(text='',86 pos=center,87 background_color=color)88 rect.set_size((int(end_point[0] - start_point[0]), int(end_point[1] - start_point[1])))89 rect.set_fill(False)90 rect.set_border_width(1)91 rect.set_disabled(True)92 self.__detection_widgets.append(rect)93 # Draw center point94 center = Button(text='', pos=center, background_color=color)95 center.set_size((2, 2))96 center.set_disabled(True)97 self.__detection_widgets.append(center)98 # Draw label and score99 category = detection.categories[0]100 class_name = category.label101 probability = round(category.score, 2)102 result_text = class_name + ' (' + str(probability) + ')'103 margin = 10 # pixels104 row_size = 10 # pixels105 label = Label(text=result_text,106 pos=(int(margin + detection.bounding_box.left),107 int(margin + row_size + detection.bounding_box.top)),108 font_size=0.5,109 font_thickness=1,110 text_color=color,111 align=GUIConsts.TextAlign.LEFT)112 self.__detection_widgets.append(label)113 self.__gui.add_widgets((rect, center, label))...
canvas.py
Source:canvas.py
...39 self.current_color = color40 self.under_mouse_color: Color = canvas.matrix[0][0]41 async def on_mouse_down(self, event: events.MouseDown):42 ...43 async def on_mouse_up(self, event: events.MouseUp):44 ...45 async def on_mouse_move(self, event: events.MouseMove):46 ...47 async def on_click(self, event: events.Click):48 ...49 async def on_leave(self, event):50 ...51 def set_color(self, color: Color):52 self.current_color = color53 async def show_on_grid(self, xx, yy):54 y, x = self.mouse_pos55 if y != -1:56 self.canvas.matrix[y][x] = self.under_mouse_color57 self.mouse_pos = (58 yy // self.canvas.grid.value,59 xx // (2 * self.canvas.grid.value),60 )61 y, x = self.mouse_pos62 self.under_mouse_color = self.canvas.matrix[y][x]63 self.canvas.matrix[y][x] = self.current_color64 await self.canvas.emit(MouseStatus(self.canvas, self.mouse_pos))65 self.canvas.update()66class ToolPaint(Tool):67 is_painting: bool = False68 def __init__(self, canvas, color) -> None:69 super().__init__(canvas, color)70 async def on_mouse_down(self, event: events.MouseDown):71 self.is_painting = True72 async def on_mouse_up(self, event: events.MouseUp):73 self.is_painting = False74 async def on_mouse_move(self, event: events.MouseMove):75 if self.is_painting:76 self.under_mouse_color = self.current_color77 await self.show_on_grid(event.x, event.y)78 async def on_click(self, event: events.Click):79 self.under_mouse_color = self.current_color80 async def on_leave(self, event):81 y, x = self.mouse_pos82 self.canvas.matrix[y][x] = self.under_mouse_color83 self.mouse_pos = (-1, -1)84 self.canvas.update()85class ToolPick(Tool):86 async def on_mouse_down(self, event: events.MouseDown):87 ...88 async def on_mouse_up(self, event: events.MouseUp):89 ...90 async def on_mouse_move(self, event: events.MouseMove):91 await self.show_on_grid(event.x, event.y)92 await self.canvas.emit(93 ColorUnderMouse(self.canvas, False, self.under_mouse_color)94 )95 async def on_click(self, event: events.Click):96 await self.canvas.emit(97 ColorUnderMouse(self.canvas, True, self.under_mouse_color)98 )99 async def on_leave(self, event):100 y, x = self.mouse_pos101 self.canvas.matrix[y][x] = self.under_mouse_color102 self.mouse_pos = (-1, -1)103 await self.canvas.emit(ColorStatus(self, {}))104 self.canvas.update()105class Tools(Enum):106 PAINT = ToolPaint107 PICK = ToolPick108class Canvas(Widget):109 """The digital display of the calculator."""110 matrix: Reactive[List[List[Color]]] = Reactive([[]])111 mouse_pos: tuple = (0, 0)112 current_color: Color = Color.from_rgb(0, 0, 0)113 is_painting: bool = False114 def __init__(115 self, w: int = 60, h: int = 60, grid: Grid = Grid.g8x8, *args, **kwargs116 ) -> None:117 super().__init__(*args, **kwargs)118 self.grid = grid119 self.w = w // 2120 self.h = h // 2121 self.matrix = [122 [Color.from_rgb(0, 0, 32 + 32 * ((-1) ** (y + x))) for y in range(h)]123 for x in range(w)124 ]125 self.under_mouse_color = self.matrix[0][0]126 self.tool: Tool = ToolPaint(self, self.current_color)127 def set_tool(self, tool: Tools):128 self.tool = tool.value(self, self.current_color)129 async def on_mouse_move(self, event: events.MouseMove) -> None:130 await self.tool.on_mouse_move(event)131 async def on_click(self, event: events.Click) -> None:132 await self.tool.on_click(event)133 async def on_mouse_down(self, event: events.MouseDown) -> None:134 await self.tool.on_mouse_down(event)135 async def on_mouse_up(self, event: events.MouseUp) -> None:136 await self.tool.on_mouse_up(event)137 async def on_leave(self, event):138 await self.tool.on_leave(event)139 def set_color(self, color: Color):140 self.current_color = color141 self.tool.set_color(color)142 def update(self):143 self.refresh()144 def render(self) -> RenderableType:145 return self146 def set_matrix(self):147 w, h = self.w * 2, self.h * 2148 self.matrix = [149 [Color.from_rgb(0, 0, 32 + 32 * ((-1) ** (y + x))) for y in range(h)]150 for x in range(w)...
button.py
Source:button.py
...22 self.__hover = False23 def click(self):24 if self.__on_click:25 self.__on_click(self)26 def on_mouse_up(self):27 if self.__on_mouse_up:28 self.__on_mouse_up()29 def on_mouse_down(self):30 if self.__on_mouse_down:31 self.__on_mouse_down()32 def is_hover(self):33 return self.__hover34 def set_disabled(self, disabled: bool):35 self.__disabled = disabled36 def is_disabled(self):37 return self.__disabled38 def aabb(self):39 (w, h), baseline = self._measurements40 if self._size is None:41 left_top = (self._pos[0] - w // 2 - self.__padding, self._pos[1] - h // 2 - self.__padding)42 right_bottom = (self._pos[0] + w // 2 + self.__padding, self._pos[1] + h // 2 + self.__padding)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!