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