Project

General

Profile

Codigo Utilizado » History » Version 8

fernando diaz, 10/22/2024 09:03 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 8 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 8 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 1 fernando diaz
        imagenes1 = ["flechaFront.png", "flechaBack.png", "flechaLeft.png", "flechaRight.png"]
38 1 fernando diaz
        self.images1 = [tk.PhotoImage(file=imagen).subsample(25, 25) for imagen in imagenes1]
39 8 fernando diaz
        self.flechaFront, self.flechaBack, self.flechaLeft, self.flechaRight= self.images1
40 8 fernando diaz
        
41 1 fernando diaz
    def init_elements(self):
42 8 fernando diaz
        btn_color = "#fff" if self.STATUS_CONNECTION else "#000" 
43 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)
44 8 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)
45 8 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)
46 8 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)
47 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)
48 8 fernando diaz
        self.btn_Grab = tk.Button(self, image=self.garra_abierta_img, bd=0, command=lambda: self.move_grab_car(None)).place(x=545, y=200)
49 8 fernando diaz
        
50 2 fernando diaz
    def ip_connect(self):
51 1 fernando diaz
        if not self.STATUS_CONNECTION:
52 1 fernando diaz
            ventana_conexion = tk.Toplevel(self)
53 1 fernando diaz
            ventana_conexion.title("Ventana de Conexión")
54 1 fernando diaz
            ventana_conexion.geometry("200x150")
55 1 fernando diaz
            ventana_conexion.resizable(width=False, height=False)
56 1 fernando diaz
            etiqueta = tk.Label(ventana_conexion, text="Ingrese la dirección IP:")
57 1 fernando diaz
            etiqueta.pack(pady=10)
58 1 fernando diaz
            self.IP = tk.StringVar()
59 1 fernando diaz
            entry_ip = tk.Entry(ventana_conexion, textvariable=self.IP)
60 1 fernando diaz
            entry_ip.pack(pady=10)
61 1 fernando diaz
            btn_conectar = tk.Button(ventana_conexion, text="Conectar", command= lambda: self.robot_connection(self.IP.get()))
62 1 fernando diaz
            btn_conectar.pack(pady=10)
63 1 fernando diaz
        else:
64 1 fernando diaz
            self.robot_connection(self.IP.get())
65 1 fernando diaz
66 1 fernando diaz
    def robot_connection(self, IP):
67 1 fernando diaz
        if self.STATUS_CONNECTION:
68 3 fernando diaz
            self.STATUS_CONNECTION = not self.STATUS_CONNECTION
69 1 fernando diaz
            self.init_elements()  
70 1 fernando diaz
        else:
71 1 fernando diaz
            try:
72 1 fernando diaz
                self.client = socket.socket()
73 1 fernando diaz
                self.client.connect((IP, self.PORT))  
74 1 fernando diaz
                self.STATUS_CONNECTION = True
75 1 fernando diaz
                self.init_elements()  
76 1 fernando diaz
            except socket.error:
77 1 fernando diaz
                messagebox.showerror("Error", "No se pudo conectar al robot.")
78 1 fernando diaz
                self.STATUS_CONNECTION = False
79 8 fernando diaz
80 1 fernando diaz
    def move_front_car(self, event):
81 1 fernando diaz
        self.client.send(bytes([ord('w')]))
82 8 fernando diaz
83 1 fernando diaz
    def move_left_car(self, event):
84 1 fernando diaz
        self.client.send(bytes([ord('a')]))
85 8 fernando diaz
86 1 fernando diaz
    def move_right_car(self, event):
87 1 fernando diaz
        self.client.send(bytes([ord('d')]))
88 8 fernando diaz
89 1 fernando diaz
    def move_back_car(self, event):
90 1 fernando diaz
        self.client.send(bytes([ord('s')]))
91 8 fernando diaz
        
92 7 fernando diaz
    def move_grab_car(self, event):
93 7 fernando diaz
        self.client.send(bytes([ord('e')])) 
94 7 fernando diaz
        if self.grab_open:
95 7 fernando diaz
            self.grab_open = False  # Ahora la garra está cerrada
96 7 fernando diaz
            self.btn_Grab.config(image=self.garra_cerrada_img)
97 7 fernando diaz
        else:
98 7 fernando diaz
            self.grab_open = True  # Ahora la garra está abierta
99 8 fernando diaz
            self.btn_Grab.config(image=self.garra_abierta_img)
100 7 fernando diaz
101 1 fernando diaz
if __name__ == "__main__":
102 1 fernando diaz
    app = App()
103 1 fernando diaz
    app.mainloop()
104 8 fernando diaz
105 6 fernando diaz
106 6 fernando diaz
107 6 fernando diaz
108 1 fernando diaz
</code></pre>