Project

General

Profile

Código e implementación » History » Version 12

Bruno Amestica, 12/12/2023 09:03 AM

1 1 Bruno Amestica
h1. Código e implementación
2 2 Bruno Amestica
3 12 Bruno Amestica
{{collapse(View details...)
4 12 Bruno Amestica
import tkinter as tk
5 12 Bruno Amestica
from tkinter import messagebox
6 12 Bruno Amestica
import socket
7 1 Bruno Amestica
8 12 Bruno Amestica
def adelante():
9 12 Bruno Amestica
    clientSocket.send(bytes([ord('w')]))
10 12 Bruno Amestica
11 12 Bruno Amestica
def atras():
12 12 Bruno Amestica
    clientSocket.send(bytes([ord('s')]))
13 12 Bruno Amestica
14 12 Bruno Amestica
def derecha():
15 12 Bruno Amestica
    clientSocket.send(bytes([ord('d')]))
16 12 Bruno Amestica
17 12 Bruno Amestica
def izquierda():
18 12 Bruno Amestica
    clientSocket.send(bytes([ord('a')]))
19 12 Bruno Amestica
20 12 Bruno Amestica
def golpear():
21 12 Bruno Amestica
    clientSocket.send(bytes([ord('r')]))
22 12 Bruno Amestica
    
23 12 Bruno Amestica
def on_release(event):
24 12 Bruno Amestica
    clientSocket.send(bytes([ord(' ')]))
25 12 Bruno Amestica
26 12 Bruno Amestica
def abrirventana():
27 12 Bruno Amestica
    ip = entry.get()
28 12 Bruno Amestica
    try:
29 12 Bruno Amestica
        clientSocket.connect((ip,port))
30 12 Bruno Amestica
        messagebox.showinfo("Conexión exitosa","Cliente conectado al robot: {0} : {1}".format(ip,port))
31 12 Bruno Amestica
32 12 Bruno Amestica
        ventana.destroy()
33 12 Bruno Amestica
        nueva_ventana = tk.Tk()
34 12 Bruno Amestica
        nueva_ventana.title("Lego® Mindstorms EV3")
35 12 Bruno Amestica
        nueva_ventana.geometry("720x405")
36 12 Bruno Amestica
        nueva_ventana.resizable(width=False, height=False)
37 12 Bruno Amestica
        #nueva_ventana.iconbitmap(icono)
38 12 Bruno Amestica
39 12 Bruno Amestica
        canvas = tk.Canvas(nueva_ventana, width=720, height=405, highlightthickness=0)
40 12 Bruno Amestica
        canvas.pack()
41 12 Bruno Amestica
42 12 Bruno Amestica
        img = tk.PhotoImage(file="/home/proyecto1/Escritorio/interfaz/fondo2.png")
43 12 Bruno Amestica
        canvas.create_image(360, 202, anchor="center", image=img)
44 12 Bruno Amestica
45 12 Bruno Amestica
        boton_adelante = tk.Button(nueva_ventana, text="Adelante",repeatdelay=50,repeatinterval=50, command=adelante, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
46 12 Bruno Amestica
        boton_adelante.bind('<ButtonRelease-1>',on_release)
47 12 Bruno Amestica
        boton_adelante.place(x=361, y=120, anchor="center")
48 12 Bruno Amestica
49 12 Bruno Amestica
        boton_atras = tk.Button(nueva_ventana, text="Atrás",repeatdelay=50,repeatinterval=50, command=atras, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
50 12 Bruno Amestica
        boton_atras.bind('<ButtonRelease-1>',on_release)
51 12 Bruno Amestica
        boton_atras.place(x=361, y=300, anchor="center")
52 12 Bruno Amestica
53 12 Bruno Amestica
        boton_izquierda = tk.Button(nueva_ventana, text="Izquierda",repeatdelay=50,repeatinterval=50, command=izquierda, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
54 12 Bruno Amestica
        boton_izquierda.bind('<ButtonRelease-1>',on_release)
55 12 Bruno Amestica
        boton_izquierda.place(x=220, y=202, anchor="center")
56 12 Bruno Amestica
57 12 Bruno Amestica
        boton_derecha = tk.Button(nueva_ventana, text="Derecha",repeatdelay=50,repeatinterval=50, command=derecha, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
58 12 Bruno Amestica
        boton_derecha.bind('<ButtonRelease-1>',on_release)
59 12 Bruno Amestica
        boton_derecha.place(x=500, y=202, anchor="center")
60 12 Bruno Amestica
61 12 Bruno Amestica
        boton_golpear = tk.Button(nueva_ventana, text="Golpear", command=golpear, bg="#2e6c89", fg="white", font=("Helvetica", 10, "bold"))
62 12 Bruno Amestica
        boton_golpear.place(x=360, y=202, anchor="center")
63 12 Bruno Amestica
64 12 Bruno Amestica
        boton_salir = tk.Button(nueva_ventana, text="Salir", command=nueva_ventana.destroy, bg="#040c0c", fg="white", font=("Helvetica", 10, "bold"))
65 12 Bruno Amestica
        boton_salir.place(x=650, y=360)
66 12 Bruno Amestica
67 12 Bruno Amestica
        nueva_ventana.mainloop()
68 12 Bruno Amestica
    except socket.error:
69 12 Bruno Amestica
        messagebox.showwarning("Conexión erronea","No se ha logrado al conexión, verifique la ip {0}".format(ip))
70 12 Bruno Amestica
        clientSocket.close()
71 12 Bruno Amestica
72 12 Bruno Amestica
ventana = tk.Tk()
73 12 Bruno Amestica
ventana.title("Lego® Mindstorms EV3")
74 12 Bruno Amestica
ventana.geometry("720x405")
75 12 Bruno Amestica
ventana.resizable(width=False, height=False)
76 12 Bruno Amestica
#icono = "home/proyecto1/Escritorio/interfaz/lego.ico"
77 12 Bruno Amestica
#ventana.iconbitmap(icono)
78 12 Bruno Amestica
79 12 Bruno Amestica
canvas = tk.Canvas(ventana, width=720, height=405, highlightthickness=0)
80 12 Bruno Amestica
canvas.pack()
81 12 Bruno Amestica
82 12 Bruno Amestica
img = tk.PhotoImage(file="/home/proyecto1/Escritorio/interfaz/fondo.png")
83 12 Bruno Amestica
canvas.create_image(360, 202, anchor="center", image=img)
84 12 Bruno Amestica
85 12 Bruno Amestica
rect_width = 370
86 12 Bruno Amestica
rect_height = 220
87 12 Bruno Amestica
rect1 = canvas.create_rectangle(0, 0, rect_width, rect_height, fill="#0c1d25", outline="")
88 12 Bruno Amestica
canvas.place(relx=0.5, rely=0.5, anchor="center")
89 12 Bruno Amestica
90 12 Bruno Amestica
canvas.coords(rect1, (canvas.winfo_reqwidth() - rect_width) / 2,
91 12 Bruno Amestica
                        (canvas.winfo_reqheight() - rect_height) / 2,
92 12 Bruno Amestica
                        (canvas.winfo_reqwidth() + rect_width) / 2,
93 12 Bruno Amestica
                        (canvas.winfo_reqheight() + rect_height) / 2)
94 12 Bruno Amestica
95 12 Bruno Amestica
rect_width = 360
96 12 Bruno Amestica
rect_height = 210
97 12 Bruno Amestica
rect2 = canvas.create_rectangle(0, 0, rect_width, rect_height, fill="#01070a", outline="")
98 12 Bruno Amestica
canvas.place(relx=0.5, rely=0.5, anchor="center")
99 12 Bruno Amestica
100 12 Bruno Amestica
canvas.coords(rect2, (canvas.winfo_reqwidth() - rect_width) / 2,
101 12 Bruno Amestica
                        (canvas.winfo_reqheight() - rect_height) / 2,
102 12 Bruno Amestica
                        (canvas.winfo_reqwidth() + rect_width) / 2,
103 12 Bruno Amestica
                        (canvas.winfo_reqheight() + rect_height) / 2)
104 12 Bruno Amestica
105 12 Bruno Amestica
texto = canvas.create_text(360, 140, text="GOLFFENHEIMER®", font="Helvetica 16 bold", fill="white", anchor="center")
106 12 Bruno Amestica
107 12 Bruno Amestica
entry = tk.Entry(ventana, font=("Helvetica", 12), bg="#0c141b", fg="white", bd=2, relief="flat", highlightbackground="#15222e", highlightthickness=2)
108 12 Bruno Amestica
entry.place(x=360, y=202.5, anchor="center")
109 12 Bruno Amestica
110 12 Bruno Amestica
boton = tk.Button(ventana, text="Conectar", command=abrirventana, bg="#1a3c4c", fg="white", font=("Helvetica", 10, "bold"), relief=tk.FLAT)
111 12 Bruno Amestica
boton.place(x=360, y=260, anchor="center")
112 12 Bruno Amestica
113 12 Bruno Amestica
boton_salir = tk.Button(ventana, text="Salir", command=ventana.destroy, bg="#040c0c", fg="white", font=("Helvetica", 10, "bold"))
114 12 Bruno Amestica
boton_salir.place(x=650, y=360)
115 12 Bruno Amestica
116 12 Bruno Amestica
clientSocket = socket.socket()
117 12 Bruno Amestica
port = 4040
118 12 Bruno Amestica
119 12 Bruno Amestica
ventana.mainloop()
120 1 Bruno Amestica
}}