Codigo Utilizado » History » Version 3
fernando diaz, 10/18/2024 10:59 AM
1 | 2 | fernando diaz | h1. Código Utilizado |
---|---|---|---|
2 | 1 | fernando diaz | |
3 | 1 | fernando diaz | Interfaz |
4 | 2 | fernando diaz | |
5 | 2 | fernando diaz | <pre><code class="python"> |
6 | 1 | fernando diaz | import tkinter as tk |
7 | 1 | fernando diaz | from tkinter import messagebox |
8 | 1 | fernando diaz | import socket |
9 | 1 | fernando diaz | |
10 | 1 | fernando diaz | class App(tk.Tk): |
11 | 1 | fernando diaz | STATUS_CONNECTION = False |
12 | 1 | fernando diaz | PORT = 0 |
13 | 1 | fernando diaz | |
14 | 1 | fernando diaz | def __init__(self): |
15 | 1 | fernando diaz | super().__init__() |
16 | 1 | fernando diaz | self.title("Ball-e") |
17 | 1 | fernando diaz | self.config(padx=10, pady=10, bg="#FFC0CB") |
18 | 1 | fernando diaz | self.geometry("800x600") |
19 | 1 | fernando diaz | self.canvas = tk.Canvas(self, width=800, height=600) |
20 | 1 | fernando diaz | self.canvas.pack() |
21 | 1 | fernando diaz | self.resizable(width=False, height=False) |
22 | 1 | fernando diaz | self.init_elements() |
23 | 1 | fernando diaz | |
24 | 1 | fernando diaz | def init_elements(self): |
25 | 1 | fernando diaz | btn_color = "#fff" if self.STATUS_CONNECTION else "#000" |
26 | 1 | fernando diaz | self.btn_Connect = tk.Button(self, bd=0, bg=btn_color, width=40, height=40, command=self.ip_connect) |
27 | 1 | fernando diaz | self.btn_Connect.place(x=710, y=500) |
28 | 1 | fernando diaz | |
29 | 1 | fernando diaz | def ip_connect(self): |
30 | 2 | fernando diaz | if not self.STATUS_CONNECTION: |
31 | 1 | fernando diaz | ventana_conexion = tk.Toplevel(self) |
32 | 1 | fernando diaz | ventana_conexion.title("Ventana de Conexión") |
33 | 1 | fernando diaz | ventana_conexion.geometry("200x150") |
34 | 1 | fernando diaz | ventana_conexion.resizable(width=False, height=False) |
35 | 1 | fernando diaz | etiqueta = tk.Label(ventana_conexion, text="Ingrese la dirección IP:") |
36 | 1 | fernando diaz | etiqueta.pack(pady=10) |
37 | 1 | fernando diaz | self.IP = tk.StringVar() |
38 | 1 | fernando diaz | entry_ip = tk.Entry(ventana_conexion, textvariable=self.IP) |
39 | 1 | fernando diaz | entry_ip.pack(pady=10) |
40 | 1 | fernando diaz | btn_conectar = tk.Button(ventana_conexion, text="Conectar", command= lambda: self.robot_connection(self.IP.get())) |
41 | 1 | fernando diaz | btn_conectar.pack(pady=10) |
42 | 1 | fernando diaz | else: |
43 | 1 | fernando diaz | self.robot_connection(self.IP.get()) |
44 | 1 | fernando diaz | |
45 | 1 | fernando diaz | def robot_connection(self, IP): |
46 | 1 | fernando diaz | if self.STATUS_CONNECTION: |
47 | 1 | fernando diaz | self.STATUS_CONNECTION = not self.STATUS_CONNECTION |
48 | 3 | fernando diaz | self.init_elements() |
49 | 1 | fernando diaz | else: |
50 | 1 | fernando diaz | try: |
51 | 1 | fernando diaz | self.client = socket.socket() |
52 | 1 | fernando diaz | self.client.connect((IP, self.PORT)) |
53 | 1 | fernando diaz | self.STATUS_CONNECTION = True |
54 | 1 | fernando diaz | self.init_elements() |
55 | 1 | fernando diaz | except socket.error: |
56 | 1 | fernando diaz | messagebox.showerror("Error", "No se pudo conectar al robot.") |
57 | 1 | fernando diaz | self.STATUS_CONNECTION = False |
58 | 1 | fernando diaz | |
59 | 1 | fernando diaz | def move_front_car(self, event): |
60 | 1 | fernando diaz | self.client.send(bytes([ord('w')])) |
61 | 1 | fernando diaz | |
62 | 1 | fernando diaz | def move_left_car(self, event): |
63 | 1 | fernando diaz | self.client.send(bytes([ord('a')])) |
64 | 1 | fernando diaz | |
65 | 1 | fernando diaz | def move_right_car(self, event): |
66 | 1 | fernando diaz | self.client.send(bytes([ord('d')])) |
67 | 1 | fernando diaz | |
68 | 1 | fernando diaz | def move_back_car(self, event): |
69 | 1 | fernando diaz | self.client.send(bytes([ord('s')])) |
70 | 1 | fernando diaz | |
71 | 1 | fernando diaz | if __name__ == "__main__": |
72 | 1 | fernando diaz | app = App() |
73 | 1 | fernando diaz | app.mainloop() |
74 | 2 | fernando diaz | </code></pre> |