Tuesday, October 19, 2021

Remove Title Bar

no_title.py


from kivy.config import Config

Config.set('graphics', 'position', 'custom')

Config.set('graphics', 'left', 10)

Config.set('graphics', 'top', 10)


from kivy.lang import Builder

from kivymd.app import MDApp

from kivy.core.window import Window


class MainApp(MDApp):

    def build(self):

        Window.borderless = True

        self.theme_cls.theme_style = "Dark"

        self.theme_cls.primary_palette = "BlueGray"

        return Builder.load_file('no_title.kv')


MainApp().run()


no_title.kv


MDFloatLayout:

    BoxLayout:

        orientation: "vertical"

        size: root.width, root.height

        

        Label:

            text_size: self.size

            halign: "center"

            valign: "middle"

            text: "Click to close"

            font_size: 32


        Button:

            size_hint: (1, .5)

            font_size: 32

            text: "Close"

            on_press: app.stop()

Databases

first_db.py


from kivy.lang import Builder

from kivymd.app import MDApp

import sqlite3


class MainApp(MDApp):

    def build(self):

        self.theme_cls.theme_style = "Dark"

        self.theme_cls.primary_palette = "BlueGray"

        # Create Database or connect to one

        conn = sqlite3.connect('first_db.db')

        # Create a cursor

        c = conn.cursor()

        # Create a table

        c.execute("""CREATE TABLE if not exists customers(

            name text)

        """)

        # Commit changes

        conn.commit()

        # Close connection

        conn.close()

        

        return Builder.load_file('first_db.kv')

    def submit(self):

        conn = sqlite3.connect('first_db.db')

        c = conn.cursor()

        # Add a record

        c.execute("INSERT INTO customers VALUES (:first)",

            {

                'first': self.root.ids.word_input.text,

            })

        # Add a message

        self.root.ids.word_label.text = f'{self.root.ids.word_input.text} Added'

        # Clear input box

        self.root.ids.word_input.text = ''

        conn.commit()

        conn.close()

 def show_records(self):

        conn = sqlite3.connect('first_db.db')

        c = conn.cursor()

        # Grab records from database

        c.execute("SELECT * FROM customers")

        records = c.fetchall()

        word = ''

        # Loop thru records

        for record in records:

            word = f'{word}\n{record[0]}'

            self.root.ids.word_label.text = f'{word}'


        conn.commit()

        conn.close()


MainApp().run()


first_db.kv

MDFloatLayout:

    BoxLayout:

        orientation: "vertical"

        size: root.width, root.height

        

        Label:

            id: word_label

            text_size: self.size

            halign: "center"

            valign: "middle"

            text: "Enter Name"

            font_size: 32

        TextInput:

            id: word_input

            multiline: False

            size_hint: (1, .5)

        Button:

            size_hint: (1, .5)

            font_size: 32

            text: "Submit"

            on_press: app.submit()

        Button:

            size_hint: (1, .5)

            font_size: 32

            text: "Show Records"

            on_press: app.show_records()

  

Data Tables

 

table2.py


from kivy.lang import Builder

from kivymd.app import MDApp

from kivymd.uix.screen import Screen

from kivymd.uix.datatables import MDDataTable

from kivy.metrics import dp


class MainApp(MDApp):

    def build(self):

        # Define Screen

        screen = Screen()

        # Define Table

        table = MDDataTable(

            pos_hint = {'center_x': 0.5, 'center_y':0.5},

            size_hint = (0.9, 0.6),

            check = True,

            use_pagination = True,

            rows_num = 3,

            pagination_menu_height = '240dp',

            column_data = [

                ('First Name', dp(30)),

                ('Last Name', dp(30)),

                ('Email Address', dp(30)),

                ('Phone Number', dp(30))

            ],

            row_data = [

                ("Tommy", "Smith", "a@a.com", "123"),

                ("Laura", "Smith", "b@a.com", "456"),

                ("Tommy1", "Smith", "a@a.com", "123"),

                ("Laura1", "Smith", "b@a.com", "456"),

                ("Tommy2", "Smith", "a@a.com", "123"),

                ("Laura2", "Smith", "b@a.com", "456"),

                ("Tommy3", "Smith", "a@a.com", "123"),

                ("Laura3", "Smith", "b@a.com", "456"),

                ("Tommy4", "Smith", "a@a.com", "123"),

                ("Laura4", "Smith", "b@a.com", "456"),

                ("Tommy", "Smith", "a@a.com", "123"),

                ("Laura", "Smith", "b@a.com", "456"),

                ("Tommy1", "Smith", "a@a.com", "123"),

                ("Laura1", "Smith", "b@a.com", "456"),

                ("Tommy2", "Smith", "a@a.com", "123")

            ]

        )

        # Bind the Table

        table.bind(on_check_press=self.checked)

        table.bind(on_row_press=self.row_checked)



        self.theme_cls.theme_style = "Light"

        self.theme_cls.primary_palette = "BlueGray"

        # Add table widget to screen

        screen.add_widget(table)

        return screen


    # Function for check presses

    def checked(self, instance_table, current_row):

        print(instance_table, current_row)


    # Function for row presses

    def row_checked(self, instance_table, instance_row):

        print(instance_table, instance_row)


MainApp().run()

Sunday, October 17, 2021

Time Picker

 


time.py

from kivy.lang import Builder

from kivymd.app import MDApp

from kivymd.uix.pickers import MDTimePicker


class MainApp(MDApp):

    def build(self):

        #self.theme_cls.theme_style = "Light"

        #self.theme_cls.primary_palette = "BlueGray"

        return Builder.load_file('time.kv')


    # Click time

    def get_time(self, instance, time):

        self.root.ids.time_label.text = str(time)


    # Click Cancel

    def on_cancel(self, instance, time):

        self.root.ids.time_label.text = "Clicked Cancel"


    def show_time_picker(self):

        time_dialog = MDTimePicker()

        time_dialog.bind(time=self.get_time, on_cancel=self.on_cancel)

        time_dialog.open()


MainApp().run()


time.kv

MDFloatLayout:


    MDRaisedButton:

        text: "Time Picker"

        pos_hint: {'center_x': .5, 'center_y': .5}

        on_release: app.show_time_picker()


    MDLabel:

        id: time_label

        text: "Some Stuff"

        pos_hint: {'center_x': .95, 'center_y': .3}



Date Picker

 



date.py

from kivy.lang import Builder

from kivymd.app import MDApp

from kivymd.uix.pickers import MDDatePicker


class MainApp(MDApp):

    def build(self):

        self.theme_cls.theme_style = "Light"

        self.theme_cls.primary_palette = "BlueGray"

        return Builder.load_file('date.kv')


    # Click OK date

    def on_save(self, instance, value, date_range):

        self.root.ids.my_label.text = str(value)


    # Click OK date Range

    def on_save_range(self, instance, value, date_range):

        self.root.ids.my_label.text = \

            f'{str(date_range[0])} thru {str(date_range[-1])}'

        

    # Click Cancel

    def on_cancel(self, instance, value):

        self.root.ids.my_label.text = "Clicked Cancel"


    def show_date_picker(self):

        date_dialog = MDDatePicker()

        date_dialog.bind(on_save=self.on_save, on_cancel=self.on_cancel)

        date_dialog.open()


    def show_date_range_picker(self):

        date_dialog = MDDatePicker(mode='range')

        date_dialog.bind(on_save=self.on_save_range, on_cancel=self.on_cancel)

        date_dialog.open()


MainApp().run()


date.kv

MDFloatLayout:

    MDRaisedButton:
        text: "Date Picker"
        pos_hint: {'center_x': .5, 'center_y': .5}
        on_release: app.show_date_picker()

    MDRaisedButton:
        text: "Date Range Picker"
        pos_hint: {'center_x': .5, 'center_y': .4}
        on_release: app.show_date_range_picker()

    MDLabel:
        id: my_label
        text: "Some Stuff"
        pos_hint: {'center_x': .95, 'center_y': .3}

Alert Dialog Boxes

 

Click Alert Popup button

Click Yes, Neat button

alert.py


from kivy.lang import Builder

from kivymd.app import MDApp

from kivymd.uix.dialog import MDDialog

from kivymd.uix.button import MDFlatButton, MDRectangleFlatButton


class MainApp(MDApp):

    dialog = None

    def build(self):

        self.theme_cls.theme_style = "Dark"

        self.theme_cls.primary_palette = "BlueGray"

        return Builder.load_file('alert.kv')


    def show_alert_dialog(self):

        if not self.dialog:

            self.dialog = MDDialog(

                title = "Pretty Neat",

                text = "Some Text",

                buttons =[

                    MDFlatButton(

                        text="Cancel",

                        text_color=self.theme_cls.primary_color,

                        on_release = self.close_dialog

                        ), 

                    MDRectangleFlatButton(

                        text="Yes, Neat",

                        text_color=self.theme_cls.primary_color,

                        on_release = self.neat_dialog

                        ),

                    ],

                )

        self.dialog.open()


    # Click Cancel Button

    def close_dialog(self, obj):

        # Close alert box

        self.dialog.dismiss()


    # Click Neat Button

    def neat_dialog(self, obj):

        # Close alert box

        self.dialog.dismiss()

        # Change label text

        self.root.ids.my_label.text = "Yup"


MainApp().run()


alert.kv

BoxLayout:
    orientation: 'vertical'

    MDScreen:
        MDRectangleFlatButton:
            text: "Alert Popup"
            pos_hint: {'center_x': .5, 'center_y': .5}
            on_release: app.show_alert_dialog()

        MDLabel:
            id: my_label
            text: "Some Stuff"
            pos_hint: {'center_x': .95, 'center_y': .4}

Speed Dial Button Menu

 



sd.py


from kivy.lang import Builder

from kivymd.app import MDApp



class MainApp(MDApp):


    data = {"Python": "language-python",

        "Ruby": "language-ruby",

        "JS": "language-javascript"

    }


    def callback(self,instance):

        if (instance.icon == "language-python"):

            lan = "Python"

        if (instance.icon == "language-ruby"):

            lan = "Ruby"

        if (instance.icon == "language-javascript"):

            lan = "JS"

        self.root.ids.my_label.text = f'You Pressed {lan}'


    def open(self):

        self.root.ids.my_label.text = "Open"


    def close(self):

        self.root.ids.my_label.text = "Close"


    def build(self):

        self.theme_cls.theme_style = "Dark"

        self.theme_cls.primary_palette = "BlueGray"

        return Builder.load_file('sd.kv')



MainApp().run()


sd.kv

BoxLayout:

    orientation: 'vertical'


    MDScreen:

        MDLabel:

            id: my_label

            text: "Some Stuff"

            halign: "center"


        MDFloatingActionButtonSpeedDial:

            data: app.data

            root_button_anim: True

            #label_text_color: 0,0,1,1 #text

            #bg_color_stack_button: 0,0,1,1 #icon background

            #bg_color_root_button: 0,0,1,1 #speed dial background

            #color_icon_root_button: 0,0,1,1 #speed dial text

            #color_icon_stack_button: 0,0,1,1 #icon color


            # Pushing the buttons

            callback: app.callback


            # Open or close

            on_open: app.open()

            on_close: app.close()

NavBar with Buttons

 

bbar.py


from kivy.lang import Builder

from kivymd.app import MDApp



class MainApp(MDApp):

    def build(self):

        self.theme_cls.theme_style = "Dark"

        self.theme_cls.primary_palette = "BlueGray"

        return Builder.load_file('bbar.kv')


MainApp().run()


bbar.kv


BoxLayout:

    orientation: 'vertical'


    MDToolbar:

        title: "Bottom Navbar"

        #md_bg_color: .2,.2,.2,1

        #specific_text_color: 0,1,0,1

    

    MDBottomNavigation:

        #panel_color: 0,1,0,.5

    

        MDBottomNavigationItem:

            name: "screen1"

            text: 'Python'

            icon: 'language-python'

            MDLabel:

                text: 'Python'

                halign: 'center'


        MDBottomNavigationItem:

            name: "screen2"

            text: 'Youtube'

            icon: 'youtube'

            MDLabel:

                text: 'Youttube Channel'

                halign: 'center'

        

        MDBottomNavigationItem:

            name: "screen3"

            text: 'Instagram'

            icon: 'instagram'

            MDLabel:

                text: 'My Instagram'

                halign: 'center'


Notes:

Background color

     #md_bg_color: .2,.2,.2,1

Text color

    #specific_text_color: 0,1,0,1

Panel background color

    #panel_color: 0,1,0,.5

Icons can be found here:

http://zavoloklom.github.io/material-design-iconic-font/icons.html

or

https://materialdesignicons.com/

Bottom Bar Button


 bbutton.py


from kivy.lang import Builder

from kivymd.app import MDApp



class MainApp(MDApp):

    def build(self):

        self.theme_cls.theme_style = "Dark"

        self.theme_cls.primary_palette = "BlueGray"

        return Builder.load_file('bbutton.kv')


    def presser(self):

        self.root.ids.my_label.text= "OK"


    def presser2(self):

        self.root.ids.my_label.text= "What"


MainApp().run()



bbutton.kv


MDBoxLayout:

    orientation: 'vertical'


    MDToolbar:

        title: "Our Top Toolbar"

        left_action_items: [["menu"]]

        right_action_items: [["dots-vertical"]]


    MDLabel:

        id: my_label

        text: "Some Stuff"

        halign: 'center'

    

    MDBottomAppBar:

        MDToolbar:

            icon: 'git'

            titlr: "Bottom Menu"

            left_action_items: 

                [["menu", lambda x: app.presser2()]]

            type: 'bottom'

            #mode: 'free-end'

            mode: 'end'

            #mode: 'center'

            #mode: 'freecenter'


            on_action_button: app.presser()

Login App

 



login.py


from kivy.lang import Builder

from kivymd.app import MDApp



class MainApp(MDApp):

    def build(self):

        self.theme_cls.theme_style = "Dark"

        self.theme_cls.primary_palette = "BlueGray"

        return Builder.load_file('login.kv')

    def logger(self):

        if(self.root.ids.user.text != ""):

            self.root.ids.welcome_label.text = \

                f'Hello {self.root.ids.user.text}!'

    def clear(self):

        self.root.ids.welcome_label.text = 'WELCOME'

        self.root.ids.user.text = ''

        self.root.ids.password.text = ''


MainApp().run()


login.kv


Screen:

    MDCard:
        size_hint: None, None
        size: 300,400
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        elevation: 10
        padding: 25
        spacing: 25
        orientation: 'vertical'

        MDLabel:
            id: welcome_label
            text: "WELCOME"
            font_size: 40
            halign: 'center'
            size_hint_y: None
            height: self.texture_size[1]
            padding_y:15

        MDTextFieldRound:
            id:user
            hint_text: "username"
            icon_right: "account"
            size_hint_x: None
            width: 200
            font_size: 18
            pos_hint: {"center_x": 0.5}

        MDTextFieldRound:
            id:password
            hint_text: "password"
            icon_right: "eye-off"
            size_hint_x: None
            width: 200
            font_size: 18
            pos_hint: {"center_x": 0.5}
            password: True

        MDRoundFlatButton:
            text: "Log In"
            font_size: 12
            pos_hint: {"center_x": 0.5}
            on_press: app.logger()

        MDRoundFlatButton:
            text: "Clear"
            font_size: 12
            pos_hint: {"center_x": 0.5}
            on_press: app.clear()

        Widget:
            size_hint_y: None
            height: 10    

Color Theme

color_theme.py


from kivy.lang import Builder

from kivymd.app import MDApp



class MainApp(MDApp):

    def build(self):

        self.theme_cls.theme_style = "Dark"

        self.theme_cls.primary_palette = "Blue"

        #self.theme_cls.accent_palette = "Red"

        return Builder.load_file('color_theme.kv')


#'Red', 'Pink', 'Purple', 'DeepPurple',

#'Indigo', 'Blue', 'LightBlue', 'Cyan',

#'Teal', 'Green', 'LightGreen', 'Lime',

#'Yellow', 'Amber', 'Orange', 'DeepOrange',

#'Brown', 'Gray', "BlueGray"


MainApp().run()



color_theme.kv


Screen:


    MDRaisedButton:

        text:"Light Button"

        pos_hint: {"center_x": 0.5, "center_y": 0.7}

        md_bg_color: app.theme_cls.primary_light


    MDRaisedButton:

        text:"Second Button"

        pos_hint: {"center_x": 0.5, "center_y": 0.5}

    

    MDRaisedButton:

        text:"Third Button"

        pos_hint: {"center_x": 0.5, "center_y": 0.3}

        md_bg_color: app.theme_cls.primary_dark


Notes:

The colors used in theme_cls are listed.

accent_palette is used on some widgets… to be seen...


primary_light is a lighter shade of the theme color.

primary_dark is a darker shade of the theme color.


Useful Things

Things to type in the Terminal



list what is in a folder


    ls


Go back a folder


    cd../


Go to home folder


    cd


Deactivate the virtual environment


    deactivate




Where to begin:


{name} is the name of the program to run


    cd Desktop/kivymd

    source virt/bin/activate

    python {name}.py



    

KivyMD Installation

To Install:

Create kivymd folder on desktop

Open the Terminal

Had to install python3.8-venv   

        apt install python3.8-venv

Change directories to the new kivymd folder

        cd Desktop/kivymd

Create a virtual environment called virt 

        python -m venv virt

Activate the virtual environment

        source virt/bin/activate

Install Pillow

        pip install pillow

Install Kivy

        pip install kivy

Check install

        pip freeze

Change directories to the new KivyMD folder

        cd KivyMD

Install KivyMD (Note the period at the end)

        pip install .

Change directories to the demos folder

        cd demos

Change directories to the kitchen_sink folder

        cd kitchen_sink

List what is in that folder

        ls

Run Main.py. This will show the kitchen sink for KivyMD

        python main.py