Codigo Utilizado » History » Version 5
« Previous -
Version 5/21
(diff) -
Next » -
Current version
fernando diaz, 10/18/2024 11:01 AM
Código Utilizado¶
Interfaz Gráfica
import tkinter as tk
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.init_elements()
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)
self.btn_Connect.place(x=710, y=500)
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()