Código de la interfaz¶
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
import socket
import time
import sys
# Funciones ------------------------------------
def up(event = None):
clientSocket.send(bytes([ord('w')]))
def down(event = None):
clientSocket.send(bytes([ord('s')]))
def left(event = None):
clientSocket.send(bytes([ord('a')]))
def right(event = None):
clientSocket.send(bytes([ord('d')]))
def StrongArm(event = None):
clientSocket.send(bytes([ord('f')]))
def WeakArm(event = None):
clientSocket.send(bytes([ord('p')]))
def BackArm(event = None):
clientSocket.send(bytes([ord('i')]))
def on_realese(event):
print("click realese")
clientSocket.send(bytes([ord(' ')]))
def getAddres():
w_ip = Tk()
w_ip.geometry("300x100")
w_ip.resizable(False, False)
w_ip.grab_set()
dato = StringVar(w_ip)
w_ip.title("Ingrese dirección IP")
ip = ttk.Entry(w_ip,textvariable=dato).place(x=10,y=10)
button = Button(w_ip,text = "Conectar",command=lambda:[conectar(dato.get()),w_ip.destroy()]).place(x=170,y=9)
print(dato.get())
def conectar(adress):
port = 19999
try:
clientSocket.connect((adress,port))
messagebox.showinfo("Mensaje Servido","Cliente conectado al robot: {0} : {1}".format(adress,port))
except socket.error:
messagebox.showwarning("Conexión erronea","No se ha logrado al conexíon, verifique la Ip {0}".format(adress))
clientSocket.close()
# -------------------------------------------------
window = Tk()
window.geometry("600x500")
window.resizable(False, False)
# Imágenes ----------------------------------------
Arriba = PhotoImage(file="icono.gif")
Abajo = PhotoImage(file="iconoabajo.gif")
Derecha = PhotoImage(file="iconoderecha.gif")
Izquierda = PhotoImage(file="iconoizquierda.gif")
Fondo = PhotoImage(file="fondo.gif")
Wifi = PhotoImage(file="wifi.gif")
Mas_Rapido = PhotoImage(file="muyrapido.gif")
Rapido = PhotoImage(file="rapido.gif")
Logo = PhotoImage(file="LogoGolf2.png")
Atras = PhotoImage(file="rapido2.png")
Apagar = PhotoImage(file="Apagar.gif")
#--------------------------------------------------
fondo = Label(window, width=1500, height=1000, image=Fondo)
fondo.pack()
Golf = Label(window, width=200, height=40, image=Logo, bg="blue4").place(x=200,y=100)
# Botón Avanzar
boton_avanzar = Button(window, repeatdelay = 50, repeatinterval = 50, image = Arriba,
command = up, height = 50, width = 60,bg = "blue4")
boton_avanzar.place(x=90,y=140)
boton_avanzar.bind('<ButtonRelease-1>',on_realese)
# Botón Retroceder
boton_retroceder = Button(window, repeatdelay = 50, repeatinterval = 50,image = Abajo,
command = down, height = 50, width = 60,bg = "blue4")
boton_retroceder.place(x=90,y=260)
boton_retroceder.bind('<ButtonRelease-1>',on_realese)
# Botón derecha
boton_derecha = Button(window, repeatdelay = 50, repeatinterval = 50,image = Derecha,
command = right, height = 50, width = 60, bg = "blue4")
boton_derecha.place(x=160,y=200)
boton_derecha.bind('<ButtonRelease-1>',on_realese)
# Botón Izquierda
boton_izquierda = Button(window, repeatdelay = 50, repeatinterval = 50, image = Izquierda,
command = left, height = 50, width = 60, bg = "blue4")
boton_izquierda.place(x=20,y=200)
boton_izquierda.bind('<ButtonRelease-1>',on_realese)
# Botón Lanzar
boton_lanzar = Button(window, repeatdelay = 50, repeatinterval = 50, image = Rapido,
command = WeakArm, height = 50, width = 60, bg = "blue4")
boton_lanzar.place(x=440,y=230)
boton_lanzar.bind('<ButtonRelease-1>',on_realese)
# Botón Lanzamiento Fuerte
boton_lanzarF = Button(window,repeatdelay = 50, repeatinterval = 50, image= Mas_Rapido,
command = StrongArm, height = 50, width = 60, bg = "blue4")
boton_lanzarF.place(x=480,y=160)
boton_lanzarF.bind('<ButtonRelease-1>',on_realese)
# Botón Retroceder Brazo
boton_lanzarA = Button(window,repeatdelay = 50, repeatinterval = 50, image = Atras,
command = BackArm, height = 50, width = 60, bg = "blue4")
boton_lanzarA.place(x=400,y=160)
boton_lanzarA.bind('<ButtonRelease-1>',on_realese)
# Botón Cerrar
boton_cerrar = Button(window,image= Apagar, command = window.destroy, height = 50, width = 60,
bg = "blue4").place(x=350,y=350)
# Botón Conectar
boton_conectar = Button(window,image = Wifi, command = getAddres, height = 50,width = 60, bg = "blue4").place(x=250,y=350)
clientSocket = socket.socket()
port = 1999
ipAddress = ""
window.mainloop()