Project

General

Profile

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

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

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