Tuesday, July 6, 2021

kivy 9 center element


#tutorial.kv
#:import random random

<ScatterTextWidget>:
    orientation: 'vertical'
    TextInput:
        id: textinput_id
        font_size: 150
        size_hint_y: None
        height: 200
        text: 'default'
        on_text: root.change_label_colour()
        #on_text: label_id.color = [random.random() for i in [0,1,2]] + [1]
    FloatLayout:
        Scatter:
            center: self.parent.center
            size_hint: None, None
            size: label_id.size
            canvas.after:
                Color:
                    rgba: 1, 0, 0, 0.5
                Rectangle:
                    size: self.size
                    pos: self.pos
            Label:
                id: label_id
                text: textinput_id.text
                font_size: 150
                color: root.text_colour
                size: self.texture_size
                canvas:
                    Color:
                        rgba: 0, 1, 0, 0.5
                    Rectangle:
                        pos: self.pos
                        size: self.size
    BoxLayout:
        orientation: 'horizontal'
        size_hint_y: None
        height: 150
        Label:
            id: label1
            text: textinput_id.text[:3]
            font_size: 100
            color: root.text_colour
        Label:
            id: label2
            text: textinput_id.text[-3:]
            font_size: 100
            color: root.text_colour

-----------------------
#main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import random
from kivy.graphics.vertex_instructions import (Rectangle, Ellipse, Line)
from kivy.graphics.context_instructions import Color
from kivy.properties import ListProperty, ObjectProperty

class ScatterTextWidget(BoxLayout):
    text_colour = ListProperty([1, 0, 0, 1])

    def change_label_colour(self, *args):
        colour = [random.random() for i in [0,1,2]] + [1]
        print(colour)
        self.text_colour = colour

class TutorialApp(App):
    def build(self):
        return ScatterTextWidget()

TutorialApp().run()

reference:

美国魔幻房价



Sunday, July 4, 2021

kivy 8 draw with kivy


#tutorial.kv
#:import random random

<ScatterTextWidget>:
    orientation: 'vertical'
    canvas.before:
        Color:
            rgba: 0, 0, 1, 1
        Rectangle:
            pos: 0, 100
            size: 300, 100
        Ellipse:
            pos: 0, 400
            size: 300, 100
        Line:
            points: 0, 0, 500, 600, 400,  300
            close: True
            width: 3
    TextInput:
        id: textinput_id
        font_size: 150
        size_hint_y: None
        height: 200
        text: 'default'
        on_text: root.change_label_colour()
        #on_text: label_id.color = [random.random() for i in [0,1,2]] + [1]
    FloatLayout:
        Scatter:
            Label:
                id: label_id
                text: textinput_id.text
                font_size: 150
                color: root.text_colour
    BoxLayout:
        orientation: 'horizontal'
        size_hint_y: None
        height: 150
        Label:
            id: label1
            text: textinput_id.text[:3]
            font_size: 100
            color: root.text_colour
        Label:
            id: label2
            text: textinput_id.text[-3:]
            font_size: 100
            color: root.text_colour

reference:

kivy 7 draw with python


#main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import random
from kivy.graphics.vertex_instructions import (Rectangle, Ellipse, Line)
from kivy.graphics.context_instructions import Color
from kivy.properties import ListProperty, ObjectProperty

class ScatterTextWidget(BoxLayout):
    text_colour = ListProperty([1, 0, 0, 1])

    def __init__(self, **kwargs):
        super(ScatterTextWidget, self).__init__(**kwargs)

        with self.canvas.after:
            Color(0, 1, 0, 1)
            Rectangle(pos=(0, 100), size=(300, 100))
            Ellipse(pos=(0, 400), size=(300, 100))
            Line(points=[0, 0, 500, 600, 400, 300], close=True, width=3)

    def change_label_colour(self, *args):
        colour = [random.random() for i in [0,1,2]] + [1]
        print(colour)
        self.text_colour = colour

class TutorialApp(App):
    def build(self):
        return ScatterTextWidget()

TutorialApp().run()

reference:

Saturday, July 3, 2021

kivy 6 property



#main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import random

from kivy.properties import ListProperty

class ScatterTextWidget(BoxLayout):
    text_colour = ListProperty([1, 0, 0, 1])

    def change_label_colour(self, *args):
        colour = [random.random() for i in [0,1,2]] + [1]
        print(colour)
        self.text_colour = colour

class TutorialApp(App):
    def build(self):
        return ScatterTextWidget()

TutorialApp().run()

-----------------------
#tutorial.kv
#:import random random

<ScatterTextWidget>:
    orientation: 'vertical'
    TextInput:
        id: textinput_id
        font_size: 150
        size_hint_y: None
        height: 200
        text: 'default'
        on_text: root.change_label_colour()
        #on_text: label_id.color = [random.random() for i in [0,1,2]] + [1]
    FloatLayout:
        Scatter:
            Label:
                id: label_id
                text: textinput_id.text
                font_size: 150
                color: root.text_colour
    BoxLayout:
        orientation: 'horizontal'
        size_hint_y: None
        height: 150
        Label:
            id: label1
            text: textinput_id.text[:3]
            font_size: 100
            color: root.text_colour
        Label:
            id: label2
            text: textinput_id.text[-3:]
            font_size: 100
            color: root.text_colour

reference:

Thursday, July 1, 2021

kivy 5 kivy language with python function



#project structure
main.py
tutorial.kv

#main.py
from kivy.uix.boxlayout import BoxLayout
import random

class ScatterTextWidget(BoxLayout):
    def change_label_colour(self, *args):
        colour = [random.random() for i in [0,1,2]] + [1]
        print(colour)
        label = self.ids['label_id']
        label.color = colour

class TutorialApp(App):
    def build(self):
        return ScatterTextWidget()

TutorialApp().run()

---------------------------
tutorial.kv
#:import random random

<ScatterTextWidget>:
    orientation: 'vertical'
    TextInput:
        id: textinput_id
        font_size: 150
        size_hint_y: None
        height: 200
        text: 'default'
        on_text: root.change_label_colour()
        #on_text: label_id.color = [random.random() for i in [0,1,2]] + [1]
    FloatLayout:
        Scatter:
            Label:
                id: label_id
                text: textinput_id.text
                font_size: 150

-----------------
#logs
[0.1348179395731689, 0.24583291893163917, 0.9907235899551307, 1]
[0.2874682150281308, 0.9696246742617654, 0.550108717419674, 1]

reference: