Codigo Utilizado » History » Version 7
fernando diaz, 10/22/2024 08:49 AM
1 | 2 | fernando diaz | h1. Código Utilizado |
---|---|---|---|
2 | 1 | fernando diaz | |
3 | 5 | fernando diaz | *Interfaz Gráfica* |
4 | 2 | fernando diaz | |
5 | 2 | fernando diaz | <pre><code class="python"> |
6 | 1 | fernando diaz | import tkinter as tk |
7 | 6 | fernando diaz | from tkinter import ttk |
8 | 1 | fernando diaz | from tkinter import messagebox |
9 | 1 | fernando diaz | import socket |
10 | 1 | fernando diaz | |
11 | 1 | fernando diaz | class App(tk.Tk): |
12 | 1 | fernando diaz | STATUS_CONNECTION = False |
13 | 1 | fernando diaz | PORT = 0 |
14 | 1 | fernando diaz | |
15 | 1 | fernando diaz | def __init__(self): |
16 | 1 | fernando diaz | super().__init__() |
17 | 1 | fernando diaz | self.title("Ball-e") |
18 | 1 | fernando diaz | self.config(padx=10, pady=10, bg="#FFC0CB") |
19 | 1 | fernando diaz | self.geometry("800x600") |
20 | 1 | fernando diaz | self.canvas = tk.Canvas(self, width=800, height=600) |
21 | 1 | fernando diaz | self.canvas.pack() |
22 | 1 | fernando diaz | self.resizable(width=False, height=False) |
23 | 6 | fernando diaz | self.imagen_fondo = tk.PhotoImage(file="fondo.png") |
24 | 6 | fernando diaz | self.imagen_fondo = self.imagen_fondo.subsample(5,5) |
25 | 6 | fernando diaz | self.canvas.create_image(0, 0, anchor=tk.NW, image=self.imagen_fondo) |
26 | 6 | fernando diaz | self.init_images() |
27 | 1 | fernando diaz | self.init_elements() |
28 | 7 | fernando diaz | self.grab_open = True # Inicialmente, la garra está abierta |
29 | 1 | fernando diaz | |
30 | 6 | fernando diaz | def init_images(self): |
31 | 6 | fernando diaz | imagenes = ["encendido.png"] |
32 | 6 | fernando diaz | self.images = [tk.PhotoImage(file=imagen).subsample(12, 12) for imagen in imagenes] |
33 | 6 | fernando diaz | |
34 | 7 | fernando diaz | self.garra_abierta_img = tk.PhotoImage(file="garra_abierta.png").subsample(10, 10) |
35 | 7 | fernando diaz | self.garra_cerrada_img = tk.PhotoImage(file="garra_cerrada.png").subsample(10, 10) |
36 | 7 | fernando diaz | |
37 | 6 | fernando diaz | imagenes1 = ["flechaFront.png", "flechaBack.png", "flechaLeft.png", "flechaRight.png"] |
38 | 6 | fernando diaz | self.images1 = [tk.PhotoImage(file=imagen).subsample(25, 25) for imagen in imagenes1] |
39 | 6 | fernando diaz | self.flechaFront, self.flechaBack, self.flechaLeft, self.flechaRight = self.images1 |
40 | 1 | fernando diaz | def init_elements(self): |
41 | 1 | fernando diaz | btn_color = "#fff" if self.STATUS_CONNECTION else "#000" |
42 | 6 | fernando diaz | self.btn_Connect = tk.Button(self, bd=0, bg=btn_color, width=40, height=40, command=self.ip_connect).place(x=710, y=500) |
43 | 6 | fernando diaz | self.btn_Front = tk.Button(self, image=self.flechaFront, bd=0, bg="white", highlightbackground='white', width=80, height=80, command=lambda: self.move_front_car(None)).place(x=545,y=100) |
44 | 6 | fernando diaz | self.btn_Back = tk.Button(self, image=self.flechaBack, bd=0, width=80, height=80, command=lambda:self.move_back_car(None)).place(x=545,y=300) |
45 | 6 | fernando diaz | self.btn_Left = tk.Button(self, image = self.flechaLeft, bd=0, width=80, height=80, command=lambda:self.move_left_car(None)).place(x=450,y=200) |
46 | 6 | fernando diaz | self.btn_Right = tk.Button(self, image= self.flechaRight, bd=0, width=80, height=80, command=lambda: self.move_right_car(None)).place(x=640,y=200) |
47 | 7 | fernando diaz | self.btn_Grab = tk.Button(self, image=self.garra_abierta_img, bd=0, command=lambda: self.move_grab_car(None)).place(x=350, y=500) |
48 | 6 | fernando diaz | |
49 | 2 | fernando diaz | def ip_connect(self): |
50 | 1 | fernando diaz | if not self.STATUS_CONNECTION: |
51 | 1 | fernando diaz | ventana_conexion = tk.Toplevel(self) |
52 | 1 | fernando diaz | ventana_conexion.title("Ventana de Conexión") |
53 | 1 | fernando diaz | ventana_conexion.geometry("200x150") |
54 | 1 | fernando diaz | ventana_conexion.resizable(width=False, height=False) |
55 | 1 | fernando diaz | etiqueta = tk.Label(ventana_conexion, text="Ingrese la dirección IP:") |
56 | 1 | fernando diaz | etiqueta.pack(pady=10) |
57 | 1 | fernando diaz | self.IP = tk.StringVar() |
58 | 1 | fernando diaz | entry_ip = tk.Entry(ventana_conexion, textvariable=self.IP) |
59 | 1 | fernando diaz | entry_ip.pack(pady=10) |
60 | 1 | fernando diaz | btn_conectar = tk.Button(ventana_conexion, text="Conectar", command= lambda: self.robot_connection(self.IP.get())) |
61 | 1 | fernando diaz | btn_conectar.pack(pady=10) |
62 | 1 | fernando diaz | else: |
63 | 1 | fernando diaz | self.robot_connection(self.IP.get()) |
64 | 1 | fernando diaz | |
65 | 1 | fernando diaz | def robot_connection(self, IP): |
66 | 1 | fernando diaz | if self.STATUS_CONNECTION: |
67 | 3 | fernando diaz | self.STATUS_CONNECTION = not self.STATUS_CONNECTION |
68 | 6 | fernando diaz | self.init_elements() |
69 | 1 | fernando diaz | else: |
70 | 1 | fernando diaz | try: |
71 | 1 | fernando diaz | self.client = socket.socket() |
72 | 1 | fernando diaz | self.client.connect((IP, self.PORT)) |
73 | 1 | fernando diaz | self.STATUS_CONNECTION = True |
74 | 1 | fernando diaz | self.init_elements() |
75 | 1 | fernando diaz | except socket.error: |
76 | 1 | fernando diaz | messagebox.showerror("Error", "No se pudo conectar al robot.") |
77 | 1 | fernando diaz | self.STATUS_CONNECTION = False |
78 | 1 | fernando diaz | |
79 | 1 | fernando diaz | def move_front_car(self, event): |
80 | 1 | fernando diaz | self.client.send(bytes([ord('w')])) |
81 | 1 | fernando diaz | |
82 | 1 | fernando diaz | def move_left_car(self, event): |
83 | 1 | fernando diaz | self.client.send(bytes([ord('a')])) |
84 | 1 | fernando diaz | |
85 | 1 | fernando diaz | def move_right_car(self, event): |
86 | 1 | fernando diaz | self.client.send(bytes([ord('d')])) |
87 | 1 | fernando diaz | |
88 | 1 | fernando diaz | def move_back_car(self, event): |
89 | 1 | fernando diaz | self.client.send(bytes([ord('s')])) |
90 | 7 | fernando diaz | |
91 | 7 | fernando diaz | def move_grab_car(self, event): |
92 | 7 | fernando diaz | self.client.send(bytes([ord('e')])) |
93 | 7 | fernando diaz | if self.grab_open: |
94 | 7 | fernando diaz | self.grab_open = False # Ahora la garra está cerrada |
95 | 7 | fernando diaz | self.btn_Grab.config(image=self.garra_cerrada_img) |
96 | 7 | fernando diaz | else: |
97 | 7 | fernando diaz | self.grab_open = True # Ahora la garra está abierta |
98 | 7 | fernando diaz | self.btn_Grab.config(image=self.garra_abierta_img) |
99 | 1 | fernando diaz | |
100 | 1 | fernando diaz | if __name__ == "__main__": |
101 | 1 | fernando diaz | app = App() |
102 | 1 | fernando diaz | app.mainloop() |
103 | 6 | fernando diaz | |
104 | 6 | fernando diaz | |
105 | 6 | fernando diaz | |
106 | 1 | fernando diaz | </code></pre> |