Project

General

Profile

Codigo Utilizado » History » Version 6

« Previous - Version 6/21 (diff) - Next » - Current version
fernando diaz, 10/20/2024 02:55 PM


Código Utilizado

Interfaz Gráfica

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import socket

class App(tk.Tk):
    STATUS_CONNECTION = False
    PORT = 0

    def __init__(self):
        super().__init__()
        self.title("Ball-e")
        self.config(padx=10, pady=10, bg="#FFC0CB")
        self.geometry("800x600")
        self.canvas = tk.Canvas(self, width=800, height=600)
        self.canvas.pack()
        self.resizable(width=False, height=False)
        self.imagen_fondo = tk.PhotoImage(file="fondo.png")
        self.imagen_fondo = self.imagen_fondo.subsample(5,5)
        self.canvas.create_image(0, 0, anchor=tk.NW, image=self.imagen_fondo)
        self.init_images()
        self.init_elements()

    def init_images(self):
        imagenes = ["encendido.png"]
        self.images = [tk.PhotoImage(file=imagen).subsample(12, 12) for imagen in imagenes]

        imagenes1 = ["flechaFront.png", "flechaBack.png", "flechaLeft.png", "flechaRight.png"]
        self.images1 = [tk.PhotoImage(file=imagen).subsample(25, 25) for imagen in imagenes1]
        self.flechaFront, self.flechaBack, self.flechaLeft, self.flechaRight = self.images1
    def init_elements(self):
        btn_color = "#fff" if self.STATUS_CONNECTION else "#000" 
        self.btn_Connect = tk.Button(self, bd=0, bg=btn_color, width=40, height=40, command=self.ip_connect).place(x=710, y=500)
        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)
        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)
        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)
        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)

    def ip_connect(self):
        if not self.STATUS_CONNECTION:
            ventana_conexion = tk.Toplevel(self)
            ventana_conexion.title("Ventana de Conexión")
            ventana_conexion.geometry("200x150")
            ventana_conexion.resizable(width=False, height=False)
            etiqueta = tk.Label(ventana_conexion, text="Ingrese la dirección IP:")
            etiqueta.pack(pady=10)
            self.IP = tk.StringVar()
            entry_ip = tk.Entry(ventana_conexion, textvariable=self.IP)
            entry_ip.pack(pady=10)
            btn_conectar = tk.Button(ventana_conexion, text="Conectar", command= lambda: self.robot_connection(self.IP.get()))
            btn_conectar.pack(pady=10)
        else:
            self.robot_connection(self.IP.get())

    def robot_connection(self, IP):
        if self.STATUS_CONNECTION:
            self.STATUS_CONNECTION = not self.STATUS_CONNECTION
            self.init_elements()  
        else:
            try:
                self.client = socket.socket()
                self.client.connect((IP, self.PORT))  
                self.STATUS_CONNECTION = True
                self.init_elements()  
            except socket.error:
                messagebox.showerror("Error", "No se pudo conectar al robot.")
                self.STATUS_CONNECTION = False

    def move_front_car(self, event):
        self.client.send(bytes([ord('w')]))

    def move_left_car(self, event):
        self.client.send(bytes([ord('a')]))

    def move_right_car(self, event):
        self.client.send(bytes([ord('d')]))

    def move_back_car(self, event):
        self.client.send(bytes([ord('s')]))

if __name__ == "__main__":
    app = App()
    app.mainloop()