Hello! In this tutorial, I will develop a GUI using Python, Kivy, KivyMD, and MySQL.
Kivy is a graphical user interface opensource Python library that allows you to develop multi-platform applications on Windows, macOS, Android, iOS, Linux, and Raspberry-Pi. In addition to the regular mouse and keyboard inputs, it also supports multitouch events. The applications made using Kivy will be similar across all the platforms but it also means that the applications feel or look will differ from any native application.
For this tutorial, we need 4 files:
- config.ini: where we will save MySQL connector credentials
- main.py: main python file to be called through MS Command (for Windows users) or Terminal (for Linux and Mac users)
- login.py: Kivy screen python class
- login.kv: Kivy screen UI components
The config file: config.ini
Here we will use the INI format config file to save MySQL connector credentials. We need only 4 parameters: host, database name, user, and password.
[mysql]
host=localhost
user=root
password=your_password_here
db=tutorialkivy01
The database “tutorialkivy01” has only one table name “users” containing one row of one user. Here is the SQL code to create the table and insert one row:
-- Table structure for table `users`
CREATE TABLE `users` (
`id_auto` int(11) NOT NULL,
`email` varchar(250) NOT NULL,
`password` varchar(250) NOT NULL,
`last_login` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Dumping data for table `users`
INSERT INTO `users` (`id_auto`, `email`, `password`, `last_login`) VALUES
(1, 'demo@tutorial.com', 'mypassword', '0000-00-00 00:00:00');
-- Indexes for table `users`
ALTER TABLE `users` ADD PRIMARY KEY (`id_auto`);
The main file: main.py
import os, sys
import configparser
from datetime import datetime
import kivy
kivy.require('1.0.8')
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, NoTransition
from kivymd.app import MDApp
from login import Login
#Login Screen
class LoginApp(MDApp):
def build(self):
self.manager = ScreenManager(transition=NoTransition())
self.manager.add_widget(Login(name='login'))
return self.manager
#python main function
if __name__ == '__main__':
if hasattr(sys, '_MEIPASS'):
resource_add_path(os.path.join(sys._MEIPASS))
LoginApp().run()
The main file: login.kv
#:kivy 1.0
<Login>:
BoxLayout:
id: login_layout
orientation: 'vertical'
padding: [30,30,30,30]
spacing: 30
size_hint: None, None
size: 500,500
GridLayout:
cols:2
MDLabel:
text: 'Email'
halign: 'left'
font_size: 18
MDTextField:
id: input_email
multiline:False
font_size: 28
MDLabel:
text: 'Password'
halign: 'left'
font_size: 18
MDTextField:
id: input_password
multiline:False
password:True
font_size: 28
Button:
text: "Fermer"
font_size: "18sp"
size_hint: None, None
size: 250,100
background_normal: ''
background_color: 1,0,0,1
on_release: app.stop()
Button:
text: "Fermer"
font_size: "18sp"
size_hint: None, None
size: 250,100
background_normal: ''
background_color: 0,1,0,1
on_release: root.connect()
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, SlideTransition
from kivymd.toast import toast
from datetime import datetime
import configparser
import mysql.connector
class Login(Screen):
pass
def connect(self):
#get email and password from Screen
app = App.get_running_app()
input_email = app.manager.get_screen('login').ids['input_email'].text
input_password = app.manager.get_screen('login').ids['input_password'].text
#load credentials from config.ini
config = configparser.ConfigParser()
config.read('config.ini')
host = config['mysql']['host']
user = config['mysql']['user']
password = config['mysql']['password']
dbname = config['mysql']['db']
#connect to MySQL
db = mysql.connector.connect(host=str(host), user=str(user), password=str(password), database=str(dbname))
cursor = db.cursor()
#run query to check email/password
query = "SELECT count(*) FROM users where email='"+str(input_email)+"' and password='"+str(input_password)+"'"
cursor.execute(query)
data = cursor.fetchone()
count = data[0]
#verif login/email
#if invalid
if count == 0:
toast('Invalid Login/Password')
#else, if valid
else:
toast('Login and Password are correct!')
#update last_login column
now = datetime.now()
query = "update users set last_login='"+str(now.strftime("%Y-%m-%d %H:%M:%S"))+"' where email='"+str(input_email)+"' and password='"+str(input_password)+"'"
cursor.execute(query)
db.commit()
db.close()
pass