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