Inicio
Buscar
Ingresar
Registrarse
Starfield: el juego que revolucionará el espacio y la tecnología
Hack x Crack - Comunidad de Seguridad informática
»
Programación
»
Otros
»
Interfaz de login para moviles con Kivy [Python]
Imprimir
Páginas: [
1
]
Ir Abajo
Autor
Tema: Interfaz de login para moviles con Kivy [Python] (Leído 7911 veces)
R3LI4NT
{ L4 } Geek
Mensajes: 396
El poder del usuario radica en su ANONIMATO.
Interfaz de login para moviles con Kivy [Python]
«
en:
Noviembre 07, 2021, 07:19:11 pm »
¿Qué es Kivy?
Kivy es una biblioteca de Python gratuita y de código abierto para desarrollar aplicaciones móviles y otro software de aplicación multitáctil con una interfaz de usuario natural. Se distribuye según los términos de la licencia MIT y se puede ejecutar en Android, iOS, Linux, macOS y Windows.
Al ingresar:
¿Cómo funciona?
Primero descargamos la biblioteca desde la terminal o símbolo de sistema:
pip install kivymd
Creamos un nuevo archivo con la extensión .py e importamos los siguientes módulos de la librería:
Código: Python
from
kivy.
lang
import
Builder
from
kivymd.
uix
.
button
import
MDFlatButton
from
kivymd.
uix
.
dialog
import
MDDialog
from
kivymd.
app
import
MDApp
from
kivy.
core
.
window
import
Window
Luego hay que darle un tamaño a la ventana, parecido a Tkinter:
Window.size=(X,X)
Ej: Window.size=(400,600)
Aquí es donde viene lo bueno, las siguientes opciones nos permite modificar diferentes elementos que se encuentran dentro de la ventana creada, esto incluye el icono, los buttons, los "inputs" llamados MDTextFieldRound, entre otras.
Código: Python
KV
=
'''
Screen:
MDCard:
size_hint: None, None
size: 300,450
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 67
spacing: 36
orientation: 'vertical'
border_radius: 20
radius: [15]
md_bg_color: [30/255,30/255,30/255,1]
MDIcon:
icon: 'account'
icon_color: 2, 2, 2, 2
haling: 'center'
font_size: 170
MDTextFieldRound:
id: user
icon_right: "account-check"
hint_text: "Usuario"
helper_text_mode: "on_focus"
foreground_color: 1, 1, 1, 1
size_hint_x: None
width: 220
font_size: 20
pos_hint: {"center_x": 0.5}
color_active: [1,1,1,1]
MDTextFieldRound:
id: password
password: True
icon_right: "lock"
hint_text: "Contraseña"
helper_text_mode: "on_focus"
foreground_color: 1, 1, 1, 1
size_hint_x: None
width: 220
font_size: 20
pos_hint: {"center_x": 0.5}
color_active: [1,1,1,1]
MDFillRoundFlatButton:
text: "LOGIN"
font_size: 18
pos_hint: {"center_x": 0.5}
on_press: app.login()
'''
Creamos una clase y está va a heredar de MDApp, el método o función build define los colores que va a tener nuestra pantalla:
Código: Python
class
MainApp
(
MDApp
)
:
dialog
=
None
def
build
(
self
)
:
self
.
theme_cls
.
theme_style
=
'Dark'
self
.
theme_cls
.
primary_palette
=
'Green'
self
.
theme_cls
.
accent_palette
=
"Blue"
return
Builder.
load_string
(
KV
)
Voy a crear una función llamada "login" en donde voy a darle un usuario y contraseña para entrar a X cosa, más que nada para probar que todo funciona.
Código: Python
def
login
(
self
)
:
if
self
.
root
.
ids
.
user
.
text
==
'Reliant'
and
self
.
root
.
ids
.
password
.
text
==
'1234'
:
if
not
self
.
dialog
:
#En caso de tener éxito...
self
.
dialog
=
MDDialog
(
title
=
'Login'
,
text
=
f
"¡Bienvenido {self.root.ids.user.text}!"
,
buttons
=
[
MDFlatButton
(
text
=
"ACEPTAR"
,
text_color
=
self
.
theme_cls
.
accent_color
,
on_release
=
self
.
close
)
,
]
,
)
self
.
dialog
.
open
(
)
En caso de tener éxito con el user y password se mostrará una pequeña ventana de bienvenida.
Por último, creamos una función de "close" para cerrar el button, y llamamos a la clase:
Código: Python
def
close
(
self
)
:
self
.
dialog
.
dismiss
(
)
MainApp
(
)
.
run
(
)
Código completo:
Código: Python
from
kivy.
lang
import
Builder
from
kivymd.
uix
.
button
import
MDFlatButton
from
kivymd.
uix
.
dialog
import
MDDialog
from
kivymd.
app
import
MDApp
from
kivy.
core
.
window
import
Window
Window.
size
=
(
400
,
600
)
KV
=
'''
Screen:
MDCard:
size_hint: None, None
size: 300,450
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 67
spacing: 36
orientation: 'vertical'
border_radius: 20
radius: [15]
md_bg_color: [30/255,30/255,30/255,1]
MDIcon:
icon: 'account'
icon_color: 2, 2, 2, 2
haling: 'center'
font_size: 170
MDTextFieldRound:
id: user
icon_right: "account-check"
hint_text: "Usuario"
helper_text_mode: "on_focus"
foreground_color: 1, 1, 1, 1
size_hint_x: None
width: 220
font_size: 20
pos_hint: {"center_x": 0.5}
color_active: [1,1,1,1]
MDTextFieldRound:
id: password
password: True
icon_right: "lock"
hint_text: "Contraseña"
helper_text_mode: "on_focus"
foreground_color: 1, 1, 1, 1
size_hint_x: None
width: 220
font_size: 20
pos_hint: {"center_x": 0.5}
color_active: [1,1,1,1]
MDFillRoundFlatButton:
text: "LOGIN"
font_size: 18
pos_hint: {"center_x": 0.5}
on_press: app.login()
'''
class
MainApp
(
MDApp
)
:
dialog
=
None
def
build
(
self
)
:
self
.
theme_cls
.
theme_style
=
'Dark'
self
.
theme_cls
.
primary_palette
=
'Green'
self
.
theme_cls
.
accent_palette
=
"Blue"
return
Builder.
load_string
(
KV
)
def
login
(
self
)
:
if
self
.
root
.
ids
.
user
.
text
==
'Reliant'
and
self
.
root
.
ids
.
password
.
text
==
'1234'
:
if
not
self
.
dialog
:
#En caso de tener éxito...
self
.
dialog
=
MDDialog
(
title
=
'Login'
,
text
=
f
"¡Bienvenido {self.root.ids.user.text}!"
,
buttons
=
[
MDFlatButton
(
text
=
"ACEPTAR"
,
text_color
=
self
.
theme_cls
.
accent_color
,
on_release
=
self
.
close
)
,
]
,
)
self
.
dialog
.
open
(
)
def
close
(
self
)
:
self
.
dialog
.
dismiss
(
)
MainApp
(
)
.
run
(
)
Link de iconos:
https://materialdesignicons.com/
#R3LI4NT
«
Última modificación: Noviembre 07, 2021, 09:05:29 pm por R3LI4NT
»
En línea
No contesto dudas por MP, si las tienes, las planteas en el foro.
Road To Hacking v3:
https://github.com/R3LI4NT/road-to-hacking
Imprimir
Páginas: [
1
]
Ir Arriba
Hack x Crack - Comunidad de Seguridad informática
»
Programación
»
Otros
»
Interfaz de login para moviles con Kivy [Python]
Va un mudo y le dice a un sordo: Hack x Crack usa cookies. Pues eso...
OK
Learn more