Project

General

Profile

interfaz.py

Vranika Santiago, 12/04/2022 06:44 PM

Download (4.28 KB)

 
1
import tkinter as tk
2
from tkinter import ttk
3
from tkinter import *
4
import socket 
5

    
6

    
7
#Ventana
8
ventana = tk.Tk()
9
ventana.config(width=900, height=700)
10
ventana.title("Interfaz -> Gorilla-Tank")
11
ventana.configure(bg="gray25")
12

    
13

    
14
#FUNCIONES
15
def Arriba(event= None):
16
    clientSocket.send(bytes([ord('w')]))
17
    
18
def Abajo(event=None):
19
    clientSocket.send(bytes([ord('s')]))
20
    
21
def Izquierda(event=None):
22
    clientSocket.send(bytes([ord('a')]))
23

    
24
def Derecha(event=None):
25
    clientSocket.send(bytes([ord('d')]))
26

    
27
def Disparar(event=None):
28
    clientSocket.send(bytes([ord('p')]))
29

    
30
def Angulo():
31
    if(opcion.get()=="45"):
32
        clientSocket.send(bytes([ord('i')]))
33
    elif(opcion.get() == "0"):
34
        clientSocket.send(bytes([ord('o')]))
35

    
36
def Luciano():
37
    clientSocket.send(bytes([ord('f')]))
38
    
39
def Distancia():
40
    clientSocket.send(bytes([ord('m')]))
41
def on_realese(event):
42
    print("click realese")
43
    clientSocket.send(bytes([ord(' ')]))
44
def Cannon(event):
45
    clientSocket.send(bytes([ord('l')]))
46

    
47
def getAddres():
48
        w_ip = Tk()
49
        w_ip.geometry("300x100")
50

    
51
        dato = StringVar(w_ip)
52
        w_ip.title("Configurar Ip")
53
        ip = ttk.Entry(w_ip,textvariable=dato).place(x=10,y=10)
54
        button = Button(w_ip,text =" Aplicar",command=lambda:[conectar(dato.get()),w_ip.destroy()]).place(x=170,y=9)
55
        print(dato.get())
56

    
57
def conectar(adress):
58
        port = 19999
59
        try:
60
                clientSocket.connect((adress,port))
61
                messagebox.showinfo("Mensaje Servido","Cliente conectado al robot: {0} : {1}".format(adress,port))
62
        except socket.error:
63
                messagebox.showwarning("Conexión erronea","No se ha logrado al conexíon, verifique la Ip {0}".format(adress))
64
                getAddres()
65
                clientSocket.close()
66
        
67

    
68

    
69

    
70
#ETIQUETAS
71

    
72
img2 = tk.PhotoImage(file="gorilla.png")
73
lnl_img2 = tk.Label(ventana,image = img2, bg = "gray25").place(x=80,y=0)
74

    
75

    
76
#Angulo
77
img3 = tk.PhotoImage(file="Angulo.png")
78
lnl_img3 = tk.Label(ventana,image = img3,bg = "gray25").place(x=600,y=100)
79
opcion=StringVar()
80
angulo = Spinbox(ventana,state="readonly",values=("0","45"),textvariable=opcion).place(x=600,y=270)
81

    
82

    
83
boton_angulo = Button(ventana, text="Ingresar", command=Angulo,background = "gray25",fg="white",font=("Arial Black",10)).place(x=600, y=300)
84

    
85

    
86
#Botones
87
#Boton Detener
88
img4 = tk.PhotoImage(file="ONOFF.png")
89
boton_salir = Button(ventana,image=img4, command=ventana.destroy,bg = "gray25").place(x=600, y=390)
90

    
91
#Boton Arriba
92
img_boton_arri = tk.PhotoImage(file="Arriba.png")
93
boton_arriba = tk.Button(repeatdelay=50,repeatinterval=50,image=img_boton_arri,command=Arriba, background = "gray25")
94
boton_arriba.place(x=170, y=340)
95
boton_arriba.bind('<ButtonRelease-1>',on_realese)
96
#Boton Abajo
97
img_boton_abaj = tk.PhotoImage(file="Abajo.png")
98
boton_abajo = tk.Button(repeatdelay=50,repeatinterval=50,image=img_boton_abaj,command=Abajo, background = "gray25")
99
boton_abajo.place(x=170, y=450)
100
boton_abajo.bind('<ButtonRelease-1>',on_realese)
101
#Boton Izquierda
102
img_boton_izq = tk.PhotoImage(file="Izquierda.png")
103
boton_izq = tk.Button(repeatdelay=50,repeatinterval=50,image=img_boton_izq,command=Izquierda, background = "gray25")
104
boton_izq.place(x=60, y=400)
105
boton_izq.bind('<ButtonRelease-1>',on_realese)
106
#Boton Derecha
107
img_boton_derech = tk.PhotoImage(file="Derecha.png")
108
boton_derech = tk.Button(repeatdelay=50,repeatinterval=50,image=img_boton_derech,command=Derecha, background = "gray25")
109
boton_derech.place(x=280, y=400)
110
boton_derech.bind('<ButtonRelease-1>',on_realese)
111
#Boton Disparar
112
boton_disparar = tk.Button(repeatdelay =50,repeatinterval = 50,text="Disparar 🎯",command=Disparar, background = "gray25",fg="white",font=("Arial Black",10))
113
boton_disparar.place(x=175,y=600)
114
boton_disparar.bind('<ButtonRelease-1>',Cannon)
115

    
116
#Boton Conectar
117
button_connect = tk.Button(ventana,text = "Conectar 📡",command=getAddres,bg = "gray25",fg="white",font=("Arial Black",10)).place(x=600,y=550)
118
#Boton Distancia
119
button_distancia = tk.Button(ventana,text = "Distancia 📏",command= Distancia ,bg = "gray25",fg="white",font=("Arial Black",10)).place(x=280,y=355)
120

    
121

    
122
#Boton Sonido
123
button_sonido =tk.Button(ventana,text = "GorillaTank 🤖",command=Luciano,bg = "gray25",fg="white",font=("Arial Black",10)).place(x=10,y=355)
124
clientSocket = socket.socket()
125
port = 1999
126
ipAddress = "192.168.201.160"
127

    
128

    
129

    
130
ventana.mainloop()
131

    
132

    
133