Project

General

Profile

Codigo Utilizado » History » Version 6

fernando diaz, 10/20/2024 02:55 PM

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 1 fernando diaz
    
29 6 fernando diaz
    def init_images(self):
30 6 fernando diaz
        imagenes = ["encendido.png"]
31 6 fernando diaz
        self.images = [tk.PhotoImage(file=imagen).subsample(12, 12) for imagen in imagenes]
32 6 fernando diaz
        
33 6 fernando diaz
        imagenes1 = ["flechaFront.png", "flechaBack.png", "flechaLeft.png", "flechaRight.png"]
34 6 fernando diaz
        self.images1 = [tk.PhotoImage(file=imagen).subsample(25, 25) for imagen in imagenes1]
35 6 fernando diaz
        self.flechaFront, self.flechaBack, self.flechaLeft, self.flechaRight = self.images1
36 1 fernando diaz
    def init_elements(self):
37 1 fernando diaz
        btn_color = "#fff" if self.STATUS_CONNECTION else "#000"
38 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)
39 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)
40 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)
41 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)
42 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)
43 6 fernando diaz
44 2 fernando diaz
    def ip_connect(self):
45 1 fernando diaz
        if not self.STATUS_CONNECTION:
46 1 fernando diaz
            ventana_conexion = tk.Toplevel(self)
47 1 fernando diaz
            ventana_conexion.title("Ventana de Conexión")
48 1 fernando diaz
            ventana_conexion.geometry("200x150")
49 1 fernando diaz
            ventana_conexion.resizable(width=False, height=False)
50 1 fernando diaz
            etiqueta = tk.Label(ventana_conexion, text="Ingrese la dirección IP:")
51 1 fernando diaz
            etiqueta.pack(pady=10)
52 1 fernando diaz
            self.IP = tk.StringVar()
53 1 fernando diaz
            entry_ip = tk.Entry(ventana_conexion, textvariable=self.IP)
54 1 fernando diaz
            entry_ip.pack(pady=10)
55 1 fernando diaz
            btn_conectar = tk.Button(ventana_conexion, text="Conectar", command= lambda: self.robot_connection(self.IP.get()))
56 1 fernando diaz
            btn_conectar.pack(pady=10)
57 1 fernando diaz
        else:
58 1 fernando diaz
            self.robot_connection(self.IP.get())
59 1 fernando diaz
60 1 fernando diaz
    def robot_connection(self, IP):
61 1 fernando diaz
        if self.STATUS_CONNECTION:
62 3 fernando diaz
            self.STATUS_CONNECTION = not self.STATUS_CONNECTION
63 6 fernando diaz
            self.init_elements()  
64 1 fernando diaz
        else:
65 1 fernando diaz
            try:
66 1 fernando diaz
                self.client = socket.socket()
67 1 fernando diaz
                self.client.connect((IP, self.PORT))  
68 1 fernando diaz
                self.STATUS_CONNECTION = True
69 1 fernando diaz
                self.init_elements()  
70 1 fernando diaz
            except socket.error:
71 1 fernando diaz
                messagebox.showerror("Error", "No se pudo conectar al robot.")
72 1 fernando diaz
                self.STATUS_CONNECTION = False
73 1 fernando diaz
    
74 1 fernando diaz
    def move_front_car(self, event):
75 1 fernando diaz
        self.client.send(bytes([ord('w')]))
76 1 fernando diaz
    
77 1 fernando diaz
    def move_left_car(self, event):
78 1 fernando diaz
        self.client.send(bytes([ord('a')]))
79 1 fernando diaz
    
80 1 fernando diaz
    def move_right_car(self, event):
81 1 fernando diaz
        self.client.send(bytes([ord('d')]))
82 1 fernando diaz
    
83 1 fernando diaz
    def move_back_car(self, event):
84 1 fernando diaz
        self.client.send(bytes([ord('s')]))
85 1 fernando diaz
86 1 fernando diaz
if __name__ == "__main__":
87 1 fernando diaz
    app = App()
88 1 fernando diaz
    app.mainloop()
89 6 fernando diaz
90 6 fernando diaz
91 6 fernando diaz
92 1 fernando diaz
</code></pre>