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()