Tuesday, August 31, 2021

kivy 46 perspective transform shift on touch


#python galaxy.py

from kivy.app import App
from kivy.graphics import Line, Rectangle, Color
from kivy.metrics import dp
from kivy.properties import NumericProperty, Clock
from kivy.uix.widget import Widget


class Galaxy(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.n_vlines = 11
        self.n_hlines = 9
        self.space_vlines = dp(100)
        self.speed_hl = 10
        self.step_hl = 0
        self.clock = 0.2
        self.speed_vl = 10
        self.step_vl = 0
        self.direction_vl = 1

        with self.canvas:
            self.vlines = []
            self.hlines = []
            for i in range(self.n_vlines):
                vline = Line(points=(0,0,0,100))
                self.vlines.append(vline)
            for j in range(self.n_hlines):
                hline = Line(points=(0,0,100,0))
                self.hlines.append(hline)
            self.rect_l = Rectangle(pos =(0,0), size=(0,0))
            self.rect_r = Rectangle(pos=(0, 0), size=(0, 0))


        #Clock.schedule_interval(self.update, self.clock)

    def on_size(self, *args):
        self.update(0.2)

    def on_touch_down(self, touch):
        if touch.x < self.width/2:
            self.rect_l.pos = (0, self.center_y-dp(50))
            self.rect_l.size = (dp(100), dp(100))

            self.direction_vl = abs(self.direction_vl)
            self.update(0.2)
        else:
            self.rect_r.pos = (self.width-dp(100), self.center_y - dp(50))
            self.rect_r.size = (dp(100), dp(100))

            self.direction_vl = -abs(self.direction_vl)
            self.update(0.2)

    def on_touch_up(self, touch):
        self.rect_l.size=(0,0)
        self.rect_r.size = (0, 0)

    def perspective_transform(self, x, y):
        #return x, y
        ratio = (self.height-y)/self.height
        tx = ratio*(self.center_x-x)
        ty = ratio**2
        return self.center_x-tx, 0.8*self.height*(1-ty)

    def update(self, dt):
        start_x = self.center_x - int(self.n_vlines / 2) * self.space_vlines
        shift_x = self.speed_vl * self.step_vl * dt / self.clock
        """
        if shift_x > self.width or shift_x < -self.width:
            self.direction_vl = -self.direction_vl
        """
        self.step_vl += self.direction_vl

        for i in range(self.n_vlines):
            x = start_x + self.space_vlines * i + shift_x
            x1, y1, x2, y2 = x, 0, x, self.height

            x1, y1 = self.perspective_transform(x1, y1)
            x2, y2 = self.perspective_transform(x2, y2)

            self.vlines[i].points = (x1, y1, x2, y2)

        space_hlines = self.height / self.n_hlines
        shift = self.speed_hl * self.step_hl * dt / self.clock
        if shift > space_hlines:
            self.step_hl = 0
            shift = 0
        self.step_hl += 1

        for j in range(self.n_hlines):
            y = j * space_hlines - shift
            x1, y1, x2, y2 = start_x + shift_x, y, self.width - start_x + shift_x, y

            _, y1 = self.perspective_transform(x1, y1)
            x1, _ = self.perspective_transform(x1, y1 / 0.8)
            _, y2 = self.perspective_transform(x2, y2)
            x2, _ = self.perspective_transform(x2, y2 / 0.8)

            self.hlines[j].points = (x1, y1, x2, y2)


class GalaxyApp(App):
    pass


GalaxyApp().run()

reference:

Sunday, August 29, 2021

数字人民币

kv 45 perspective transform shift 2


#galaxy.py

from kivy.app import App
from kivy.graphics import Line
from kivy.metrics import dp
from kivy.properties import NumericProperty, Clock
from kivy.uix.widget import Widget


class Galaxy(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.n_vlines = 11
        self.n_hlines = 9
        self.space_vlines = dp(100)
        self.speed_hl = 10
        self.step_hl = 0
        self.clock = 0.2
        self.speed_vl = 10
        self.step_vl = 0
        self.direction_vl = 1

        with self.canvas:
            self.vlines = []
            self.hlines = []
            for i in range(self.n_vlines):
                vline = Line(points=(0,0,0,100))
                self.vlines.append(vline)
            for j in range(self.n_hlines):
                hline = Line(points=(0,0,100,0))
                self.hlines.append(hline)

        Clock.schedule_interval(self.update, self.clock)

    def on_size(self, *args):
        pass

    def perspective_transform(self, x, y):
        #return x, y
        ratio = (self.height-y)/self.height
        tx = ratio*(self.center_x-x)
        ty = ratio**2
        return self.center_x-tx, 0.8*self.height*(1-ty)

    def update(self, dt):
        start_x = self.center_x - int(self.n_vlines / 2) * self.space_vlines
        shift_x = self.speed_vl * self.step_vl * dt / self.clock
        if shift_x > self.width or shift_x < -self.width:
            self.direction_vl = -self.direction_vl
        self.step_vl += self.direction_vl

        for i in range(self.n_vlines):
            x = start_x + self.space_vlines * i + shift_x
            x1, y1, x2, y2 = x, 0, x, self.height

            x1, y1 = self.perspective_transform(x1, y1)
            x2, y2 = self.perspective_transform(x2, y2)

            self.vlines[i].points = (x1, y1, x2, y2)

        space_hlines = self.height / self.n_hlines
        shift = self.speed_hl * self.step_hl * dt / self.clock
        if shift > space_hlines:
            self.step_hl = 0
            shift = 0
        self.step_hl += 1

        for j in range(self.n_hlines):
            y = j * space_hlines - shift
            x1, y1, x2, y2 = start_x + shift_x, y, self.width - start_x + shift_x, y

            _, y1 = self.perspective_transform(x1, y1)
            x1, _ = self.perspective_transform(x1, y1 / 0.8)
            _, y2 = self.perspective_transform(x2, y2)
            x2, _ = self.perspective_transform(x2, y2 / 0.8)

            self.hlines[j].points = (x1, y1, x2, y2)


class GalaxyApp(App):
    pass


GalaxyApp().run()

reference:

Saturday, August 28, 2021

kv 44 perspective transform shift


#galaxy.py

from kivy.app import App
from kivy.graphics import Line
from kivy.metrics import dp
from kivy.properties import NumericProperty, Clock
from kivy.uix.widget import Widget


class Galaxy(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.n_vlines = 11
        self.n_hlines = 9
        self.space_vlines = dp(100)
        self.speed_hl = 10
        self.step_hl = 0

        with self.canvas:
            self.vlines = []
            self.hlines = []
            for i in range(self.n_vlines):
                vline = Line(points=(0,0,0,100))
                self.vlines.append(vline)
            for j in range(self.n_hlines):
                hline = Line(points=(0,0,100,0))
                self.hlines.append(hline)

        Clock.schedule_interval(self.update, 0.2)

    def on_size(self, *args):
        pass

    def perspective_transform(self, x, y):
        #return x, y
        ratio = (self.height-y)/self.height
        tx = ratio*(self.center_x-x)
        ty = ratio**2
        return self.center_x-tx, 0.8*self.height*(1-ty)

    def update(self, dt):
        start_x = self.center_x - int(self.n_vlines / 2) * self.space_vlines
        for i in range(self.n_vlines):
            x = start_x + self.space_vlines * i
            x1, y1, x2, y2 = x, 0, x, self.height

            x1, y1 = self.perspective_transform(x1, y1)
            x2, y2 = self.perspective_transform(x2, y2)

            self.vlines[i].points = (x1, y1, x2, y2)

        space_hlines = self.height / self.n_hlines
        shift = self.speed_hl * self.step_hl
        if shift > space_hlines:
            self.step_hl = 0
            shift = 0
        self.step_hl += 1
        for j in range(self.n_hlines):
            y = j * space_hlines - shift
            x1, y1, x2, y2 = start_x, y, self.width - start_x, y

            _, y1 = self.perspective_transform(x1, y1)
            x1, _ = self.perspective_transform(x1, y1 / 0.8)
            _, y2 = self.perspective_transform(x2, y2)
            x2, _ = self.perspective_transform(x2, y2 / 0.8)

            self.hlines[j].points = (x1, y1, x2, y2)


class GalaxyApp(App):
    pass


GalaxyApp().run()

reference:

Friday, August 27, 2021

kv 43 perspective transform 2





#galaxy.py
from kivy.app import App
from kivy.graphics import Line
from kivy.metrics import dp
from kivy.properties import NumericProperty
from kivy.uix.widget import Widget


class Galaxy(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.n_vlines = 11
        self.n_hlines = 9
        self.space_vlines = dp(100)
        with self.canvas:
            self.vlines = []
            self.hlines = []
            for i in range(self.n_vlines):
                vline = Line(points=(0,0,0,100))
                self.vlines.append(vline)
            for j in range(self.n_hlines):
                hline = Line(points=(0,0,100,0))
                self.hlines.append(hline)

    def on_size(self, *args):
        start_x = self.center_x - int(self.n_vlines/2)*self.space_vlines
        for i in range(self.n_vlines):
            x = start_x+self.space_vlines*i
            x1, y1, x2, y2 = x, 0, x, self.height

            x1, y1 = self.perspective_transform(x1, y1)
            x2, y2 = self.perspective_transform(x2, y2)

            self.vlines[i].points = (x1, y1, x2, y2)

        space_hlines = self.height / self.n_hlines
        for j in range(self.n_hlines):
            y = j * space_hlines
            x1, y1, x2, y2 = start_x, y, self.width-start_x, y

            _, y1 = self.perspective_transform(x1, y1)
            x1, _ = self.perspective_transform(x1, y1/0.8)
            _, y2 = self.perspective_transform(x2, y2)
            x2, _ = self.perspective_transform(x2, y2/0.8)

            self.hlines[j].points = (x1, y1, x2, y2)

    def perspective_transform(self, x, y):
        #return x, y
        ratio = (self.height-y)/self.height
        tx = ratio*(self.center_x-x)
        ty = ratio**2
        return self.center_x-tx, 0.8*self.height*(1-ty)


class GalaxyApp(App):
    pass


GalaxyApp().run()

reference:

Thursday, August 26, 2021

Michelin-Starred Korean Steakhouse

集装箱肠梗阻

kv 42 perspective transform




#galaxy.py

from kivy.app import App
from kivy.graphics import Line
from kivy.metrics import dp
from kivy.properties import NumericProperty
from kivy.uix.widget import Widget


class Galaxy(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.n_vlines = 7
        self.space_vlines = dp(100)
        with self.canvas:
            self.vlines = []
            for i in range(self.n_vlines):
                vline = Line(points=(0,0,0,100))
                self.vlines.append(vline)

    def on_size(self, *args):
        start_x = self.center_x - int(self.n_vlines/2)*self.space_vlines
        for i in range(self.n_vlines):
            x = start_x+self.space_vlines*i
            x1, y1, x2, y2 = x, 0, x, self.height

            x1, y1 = self.perspective_transform(x1, y1)
            x2, y2 = self.perspective_transform(x2, y2)

            self.vlines[i].points = (x1, y1, x2, y2)

    def perspective_transform(self, x, y):
        #return x, y
        tx = (self.height-y)/self.height*(self.center_x-x)
        return self.center_x-tx, 0.8*y


class GalaxyApp(App):
    pass


GalaxyApp().run()

------------------
#galaxy.kv

Galaxy:
    BoxLayout:
        size: root.size

reference:

creative artist

Wednesday, August 25, 2021

Billionaire Lifestyle 5

kv 41 moving balls collision


#widget4.py
from kivy.app import App
from kivy.graphics import Ellipse, Color
from kivy.metrics import dp
from kivy.properties import Clock
from kivy.uix.widget import Widget
from random import random

class Widget4(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.balls_properties = []
        self.num = 10
        for i in range(self.num):
            new_ball = {
                "size": dp(50+50*random()),
                "vx": 1+9*random(),
                "vy": 1+9*random()
            }

            self.balls_properties.append(new_ball)

        with self.canvas:
            self.balls = []
            for i in range (self.num):
                Color(random(), random(), random())
                size = self.balls_properties[i]["size"]
                ball = Ellipse(pos=(0, 0), size=(size, size))
                self.balls.append(ball)

        Clock.schedule_interval(self.update, 0.2)

    def on_size(self, *args):
        for i in range(self.num):
            size = self.balls_properties[i]["size"]
            self.balls[i].pos = ((self.width-size)*random(), (self.height-size)*random())

    def update(self, dt):
        for i in range(self.num):
            x, y = self.balls[i].pos
            size = self.balls_properties[i]["size"]
            vx = self.balls_properties[i]["vx"]
            vy = self.balls_properties[i]["vy"]

            x += vx
            y += vy

            if y + size > self.height:
                y = self.height - size
                self.balls_properties[i]["vy"] = -vy
            if x + size > self.width:
                x = self.width - size
                self.balls_properties[i]["vx"] = -vx
            if y < 0:
                y = 0
                self.balls_properties[i]["vy"] = -vy
            if x < 0:
                x = 0
                self.balls_properties[i]["vx"] = -vx

            self.balls[i].pos = (x, y)

            #collision with other balls
            for j in range(i+1, self.num):
                xj, yj = self.balls[j].pos
                sizej = self.balls_properties[j]["size"]
                vxj = self.balls_properties[j]["vx"]
                vyj = self.balls_properties[j]["vy"]

                if (xj-x)**2 + (yj-y)**2 < (size/2 + sizej/2)**2:
                    print(i, j)
                    #if balls head same directory, swap velocity
                    #else reverse velocity
                    while (xj - x) ** 2 + (yj - y) ** 2 < (size / 2 + sizej / 2) ** 2:
                        x -= vx
                        y -= vy
                        xj -= vxj
                        yj -= vyj
                        self.balls[i].pos = (x, y)
                        self.balls[j].pos = (xj, yj)
                    if vx * vxj < 0:
                        self.balls_properties[i]["vx"] = -vx
                        self.balls_properties[j]["vx"] = -vxj
                    else:
                        self.balls_properties[i]["vx"] = vxj
                        self.balls_properties[j]["vx"] = vx
                    if vy * vyj < 0:
                        self.balls_properties[i]["vy"] = -vy
                        self.balls_properties[j]["vy"] = -vyj
                    else:
                        self.balls_properties[i]["vy"] = vyj
                        self.balls_properties[j]["vy"] = vy


class Widget4App(App):
    def build(self):
        return Widget4()


Widget4App().run()

reference:

Tuesday, August 24, 2021

kv 40 moving balls


#widget4.py

from kivy.app import App
from kivy.graphics import Ellipse, Color
from kivy.metrics import dp
from kivy.properties import Clock
from kivy.uix.widget import Widget
from random import random

class Widget4(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.balls_properties = []
        self.num = 10
        for i in range(self.num):
            new_ball = {
                "size": dp(10+90*random()),
                "vx": 1+9*random(),
                "vy": 1+9*random()
            }

            self.balls_properties.append(new_ball)

        with self.canvas:
            self.balls = []
            for i in range (self.num):
                Color(random(), random(), random())
                size = self.balls_properties[i]["size"]
                ball = Ellipse(pos=(0, 0), size=(size, size))
                self.balls.append(ball)

        Clock.schedule_interval(self.update, 0.05)

    def on_size(self, *args):
        for i in range(self.num):
            size = self.balls_properties[i]["size"]
            self.balls[i].pos = ((self.width-size)*random(), (self.height-size)*random())

    def update(self, dt):
        for i in range(self.num):
            x, y = self.balls[i].pos
            size = self.balls_properties[i]["size"]
            vx = self.balls_properties[i]["vx"]
            vy = self.balls_properties[i]["vy"]

            x += vx
            y += vy

            if y + size > self.height:
                y = self.height - size
                self.balls_properties[i]["vy"] = -vy
            if x + size > self.width:
                x = self.width - size
                self.balls_properties[i]["vx"] = -vx
            if y < 0:
                y = 0
                self.balls_properties[i]["vy"] = -vy
            if x < 0:
                x = 0
                self.balls_properties[i]["vx"] = -vx

            self.balls[i].pos = (x,y)


class Widget4App(App):
    def build(self):
        return Widget4()


Widget4App().run()

reference:

Monday, August 23, 2021

kv 39 moving ball

#widget3.py
from kivy.app import App
from kivy.graphics import Ellipse
from kivy.metrics import dp
from kivy.properties import Clock
from kivy.uix.widget import Widget


class Widget3(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.ball_size = dp(50)
        self.vx = 3
        self.vy = 3
        with self.canvas:
            self.ball = Ellipse(pos=self.center, size=(self.ball_size, self.ball_size))

        Clock.schedule_interval(self.update, 0.01)

    def on_size(self, *args):
        self.ball.pos = (self.center_x-self.ball_size/2, self.center_y-self.ball_size/2)

    def update(self, dt):
        x, y = self.ball.pos

        x += self.vx
        y += self.vy

        if y + self.ball_size > self.height:
            y = self.height  - self.ball_size
            self.vy = -self.vy
        if x + self.ball_size > self.width:
            x = self.width - self.ball_size
            self.vx = -self.vx
        if y < 0:
            y = 0
            self.vy = -self.vy
        if x < 0:
            x = 0
            self.vx = -self.vx

        self.ball.pos = (x,y)


class Widget3App(App):
    def build(self):
        return Widget3()


Widget3App().run()

reference:
 

Sunday, August 22, 2021

Nomad Living in a Truck

kv 38 canvas python



#widget2.py
from kivy.app import App
from kivy.graphics import Color, Rectangle, Line, Ellipse
from kivy.uix.widget import Widget
#from kivy.core.window import Window

class Widget2(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        with self.canvas:
            w = self.width
            h = self.height
            Color(0, 1, 0)
            self.rect1 = Rectangle(pos=(w/4, h/4), size=(w/2, h/2))
            Color(1, 0, 0)
            self.ellipse1 = Ellipse(pos=(w/8, h/8), size=(w/4, h/4))
            Color(1, 0, 0)
            self.ellipse2 = Ellipse(pos=(w*5/8, h/8), size=(w/4, h/4))
            Color(0, 0, 1)
            if w>h:
                self.line1 = Line(circle=(w/2, h/2, h/8), width=2)
            else:
                self.line1 = Line(circle=(w / 2, h / 2, w / 8), width=2)

        self.bind(pos=self.window_resize, size=self.window_resize)


    def window_resize(self, *args):
        w = self.width
        h = self.height
        self.rect1.pos = (w/4, h/4)
        self.rect1.size = (w/2, h/2)
        self.ellipse1.pos = (w/8, h/8)
        self.ellipse1.size = (w / 4, h / 4)
        self.ellipse2.pos = (w *5/ 8, h / 8)
        self.ellipse2.size = (w / 4, h / 4)
        if w>h:
            self.line1.circle = (w/2, h/2, h/8)
        else:
            self.line1.circle = (w / 2, h / 2, w / 8)
        self.line1.width = 2

class Widget2App(App):
    def build(self):
        return Widget2()


Widget2App().run()

reference:

window resize event

Saturday, August 21, 2021

Boston Dynamics

python 37 line 1



#widget1.kv
Widget1:
    canvas:
        Line:
            points: (0, self.height/2, self.width, self.height/2)
        Line:
            points: (self.width/2, 0, self.width/2, self.height)

        Color:
            rgba: 0, 0, 1, 1
        Line:
            rectangle: (self.width/4, self.height/4, self.width/2, self.height/2) #x, y, w, h
            width: 2

        Color:
            rgba: 1, 1, 0, 1
        Line:
            ellipse: (self.width/4, self.height/4, self.width/2, self.height/2)
            width: 2

        Color:
            rgba: 1, 0, 0, 1
        Line:
            circle: (self.width/2, self.height/2, self.height/4) if self.width > self.height else (self.width/2, self.height/2, self.width/4) #centerX, centerY, radius
            width: 2

------------------------
widget1.py
from kivy.app import App
from kivy.uix.widget import Widget


class Widget1(Widget):
    pass

class Widget1App(App):
    pass


Widget1App().run()

reference:

Thursday, August 19, 2021

共同富裕

kv36 draw ellipse rectangle



#widget1.kv
Widget1:
    canvas:
        Ellipse:
            pos: self.center_x - self.width/4, self.center_y - self.height/4
            size: self.width/2, self.height/2

        #Rectangle:
            #pos: self.center_x - self.width/4, self.center_y - self.height/4
            #size: self.width/2, self.height/2

---------------------
#widget1.py
from kivy.app import App
from kivy.uix.widget import Widget


class Widget1(Widget):
    pass

class Widget1App(App):
    pass


Widget1App().run()

referemce:

Wednesday, August 18, 2021

kv35 menu




#menu.py
from kivy.app import App
from kivy.uix.widget import Widget

class Menu(Widget):
    def toggle1(self, widget):
        widget.background_color = [0, 1, 1, 1]
        self.ids["button2"].background_color = [1, 1, 1, 1]
        self.ids["button3"].background_color = [1, 1, 1, 1]
        self.ids["screen_manager"].current = "window1"

    def toggle2(self, widget):
        widget.background_color = [0, 1, 1, 1]
        self.ids["button1"].background_color = [1, 1, 1, 1]
        self.ids["button3"].background_color = [1, 1, 1, 1]
        self.ids["screen_manager"].current = "window2"

    def toggle3(self, widget):
        widget.background_color = [0, 1, 1, 1]
        self.ids["button1"].background_color = [1, 1, 1, 1]
        self.ids["button2"].background_color = [1, 1, 1, 1]
        self.ids["screen_manager"].current = "window3"


class MenuApp(App):
    pass


MenuApp().run()

-----------------------------
#menu.kv
Menu:
    BoxLayout:
        size: root.width, root.height
        orientation: "vertical"
        BoxLayout:
            size_hint: 1, 0.2
            Button:
                id: button1
                text: "1"
                on_press: root.toggle1(self)
            Button:
                id: button2
                text: "2"
                on_press: root.toggle2(self)
            Button:
                id: button3
                text: "3"
                on_press: root.toggle3(self)
        ScreenManager:
            id: screen_manager
            Window1:
            Window2:
            Window3:

<Window1@Screen>:
    name: "window1"
    Label:
        text: "window 1"
        font_size: "150dp"

<Window2@Screen>:
    name: "window2"
    Label:
        text: "window 2"
        font_size: "150dp"

<Window3@Screen>:
    name: "window3"
    Label:
        text: "window 3"
        font_size: "150dp"

Monday, August 16, 2021

kv34 image

 
#image.kv
GridLayout:
    cols: 3
    Image:
        source: "img/5.jpg"
        allow_stretch: False
    Image:
        source: "img/5.jpg"
        allow_stretch: True
    Image:
        source: "img/5.jpg"
        allow_stretch: True
        keep_ratio: False

--------------------------
#image.py
from kivy.app import App
from kivy.uix.widget import Widget


class Image(Widget):
    pass

class ImageApp(App):
    pass


ImageApp().run()

reference:

Sunday, August 15, 2021

DaVinci Resolve

kv 33 textinput progressbar



#progressbar.kv
ProgressBarWidget:
    BoxLayout:
        size: root.width, root.height
        orientation: "vertical"
        BoxLayout:
            TextInput:
                size_hint: 1, None
                height: "120dp"
                pos_hint: {"center_y": 0.5}
                id: text1
                multiline: False
                on_text_validate: root.text_input(self)
                input_filter: "float"
                text: "50"
                font_size: "100dp"
            Label:
                text: root.text_1
                font_name: "font/LCD.ttf"
                font_size: "100dp"
        ProgressBar:
            max: 100
            value: float(root.text_1)

----------------------
#progressbar.py
from kivy.app import App
from kivy.properties import StringProperty
from kivy.uix.widget import Widget


class ProgressBarWidget(Widget):
    text_1 = StringProperty("50")

    def text_input(self, widget):
        self.text_1 = widget.text


class ProgressBarApp(App):
    pass


ProgressBarApp().run()

reference:

Saturday, August 14, 2021

kv 32 switch slider


#slider.kv
BoxLayout:
    size: root.width, root.height
    Switch:
        id: switch1
        size_hint: None, 1
        #on_active: root.on_switch_active(self)
        active: True
    Slider:
        id: slider1
        min: 0
        max: 100
        value: 50
        #on_value: ...
        orientation: "vertical"
        disabled: not switch1.active
    Label:
        text: str(int(slider1.value))
        font_name: "font/LCD.ttf"
        font_size: "200dp"

----------------------
#slider.py
from kivy.app import App
from kivy.uix.widget import Widget


class Slider(Widget):
    pass

class SliderApp(App):
    pass


SliderApp().run()

reference:

Thursday, August 12, 2021

kv 31 toggle button



#font.py
from kivy.app import App
from kivy.properties import StringProperty
from kivy.uix.widget import Widget


class Font(Widget):
    digit = StringProperty("1")
    num = 1
    enable = False

    def button_click(self):
        if self.enable:
            self.num += 1
            self.digit = str(self.num)

    def toggle(self, widget):
        print("toggle state: " + widget.state)
        if widget.state == "normal":
            widget.text = "OFF"
            self.enable = False
        else:
            widget.text = "ON"
            self.enable = True

class FontApp(App):
    pass


FontApp().run()

--------------------
#font.kv
Font:
    BoxLayout:
        size: root.width, root.height
        ToggleButton:
            text: "off"
            on_state: root.toggle(self)
            size_hint: None, 1
            width: "100dp"
            font_size: "70dp"
        Button:
            text: "Count"
            on_press: root.button_click()
            font_size: "70dp"
        Label:
            text: root.digit
            font_name: "font/LCD.ttf"
            font_size: "200dp"

reference:

国内车企大练“氢”功

Wednesday, August 11, 2021

kv 30 font


#font.kv
Font:
    BoxLayout:
        size: root.width, root.height
        Button:
            text: "Count"
            on_press: root.button_click()
            font_size: "100dp"
        Label:
            text: root.digit
            font_name: "font/LCD.ttf"
            font_size: "200dp"

-----------------
#font.py
from kivy.app import App
from kivy.base import runTouchApp
from kivy.uix.widget import Widget


class Float(Widget):
    pass

class FloatApp(App):
    def build(self):
        return Float()


FloatApp().run()

--------------------------
reference:

.ttf download

Fake Wood Decking

Monday, August 9, 2021

kv 29 scrollview 2


#stacklayout.kv
Scroll:

<Scroll@ScrollView>:
    Stacklayout:
        size_hint: 1, None
        height: self.minimum_height


<Stacklayout>:
    #left-right top-bottom
    orientation: "rl-bt"
    #padding: {"20dp", "20dp", "20dp", "20dp"}
    spacing: "20dp"

-----------------------------
from kivy.app import App
from kivy.metrics import dp
from kivy.uix.button import Button
from kivy.uix.stacklayout import StackLayout
from kivy.uix.widget import Widget


class Stacklayout(StackLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        for i in range(100):
            b = Button(text=str(i+1), size_hint=(None, None), size=(dp(100), dp(100)))
            self.add_widget(b)

class StacklayoutApp(App):
    pass


StacklayoutApp().run()

reference:

Sunday, August 8, 2021

Mazatlán Mexico

kv 28 stacklayout



#stacklayout.py
from kivy.app import App
from kivy.metrics import dp
from kivy.uix.button import Button
from kivy.uix.stacklayout import StackLayout
from kivy.uix.widget import Widget


class Stacklayout(StackLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        for i in range(20):
            b = Button(text=str(i+1), size_hint=(None, None), size=(dp(100), dp(100)))
            self.add_widget(b)

class StacklayoutApp(App):
    pass


StacklayoutApp().run()

--------------------------------
#stacklayout.kv
Stacklayout:
    #left-right top-bottom
    orientation: "rl-bt"
    #padding: {"20dp", "20dp", "20dp", "20dp"}
    spacing: "20dp"

reference:

Friday, August 6, 2021

kv 27 anchorlayout



#anchorlayout.kv
#:import random random

Anchorlayout:
    AnchorLayout:
        size: root.width, root.height
        #right, left, center
        anchor_x: "left"
        #bottom, top, center
        anchor_y: "top"
        BoxLayout:
            size_hint: .33, .33
            orientation: "horizontal"
            spacing: "10dp"
            Button:
                text: "A1"
                background_color: [random.random() for i in range(3)] + [1]
                size_hint: .5, 1
            Button:
                text: "A2"
                background_color: [random.random() for i in range(3)] + [1]
                size_hint: .5, 1

    AnchorLayout:
        size: root.width, root.height
        #right, left, center
        anchor_x: "center"
        #bottom, top, center
        anchor_y: "top"
        Button:
            text: "B"
            background_color: [random.random() for i in range(3)] + [1]
            size_hint: .2, .2

    AnchorLayout:
        size: root.width, root.height
        #right, left, center
        anchor_x: "right"
        #bottom, top, center
        anchor_y: "top"
        Button:
            text: "C"
            background_color: [random.random() for i in range(3)] + [1]
            size_hint: .2, .2

    AnchorLayout:
        size: root.width, root.height
        #right, left, center
        anchor_x: "left"
        #bottom, top, center
        anchor_y: "center"
        Button:
            text: "D"
            background_color: [random.random() for i in range(3)] + [1]
            size_hint: .2, .2

    AnchorLayout:
        size: root.width, root.height
        #right, left, center
        anchor_x: "center"
        #bottom, top, center
        anchor_y: "center"
        GridLayout:
            cols: 2
            rows: 2
            spacing: "10dp"
            size_hint: .33, .33
            Button:
                text: "E1"
                background_color: [random.random() for i in range(3)] + [1]
                size_hint: .5, .5
            Button:
                text: "E2"
                background_color: [random.random() for i in range(3)] + [1]
                size_hint: .5, .5
            Button:
                text: "E3"
                background_color: [random.random() for i in range(3)] + [1]
                size_hint: .5, .5
            Button:
                text: "E4"
                background_color: [random.random() for i in range(3)] + [1]
                size_hint: .5, .5

    AnchorLayout:
        size: root.width, root.height
        #right, left, center
        anchor_x: "right"
        #bottom, top, center
        anchor_y: "center"
        Button:
            text: "F"
            background_color: [random.random() for i in range(3)] + [1]
            size_hint: .2, .2

    AnchorLayout:
        size: root.width, root.height
        #right, left, center
        anchor_x: "left"
        #bottom, top, center
        anchor_y: "bottom"
        Button:
            text: "G"
            background_color: [random.random() for i in range(3)] + [1]
            size_hint: .2, .2

    AnchorLayout:
        size: root.width, root.height
        #right, left, center
        anchor_x: "center"
        #bottom, top, center
        anchor_y: "bottom"
        Button:
            text: "H"
            background_color: [random.random() for i in range(3)] + [1]
            size_hint: .2, .2

    AnchorLayout:
        size: root.width, root.height
        #right, left, center
        anchor_x: "right"
        #bottom, top, center
        anchor_y: "bottom"
        BoxLayout:
            size_hint: .33, .33
            orientation: "vertical"
            spacing: "10dp"
            Button:
                text: "I1"
                background_color: [random.random() for i in range(3)] + [1]
                size_hint: 1, .5
            Button:
                text: "I2"
                background_color: [random.random() for i in range(3)] + [1]
                size_hint: 1, .5

------------------
anchorlayout.py
from kivy.app import App
from kivy.uix.widget import Widget


class Anchorlayout(Widget):
    pass

class AnchorlayoutApp(App):
    pass


AnchorlayoutApp().run()

reference: