青年一代,有理想,有本领,有担当,国家就有前途,民族就有希望。 
Saturday, July 31, 2021
Friday, July 30, 2021
kv 23 login screen
#screen2.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
class MainWindow(Screen):
    pass
class SecondWindow(Screen):
    pass
class WindowManager(ScreenManager):
    pass
kv = Builder.load_file("screen2.kv")
class Screen2App(App):
    def build(self):
        return kv
Screen2App().run()
-----------------------
#screen2.kv
WindowManager:
    MainWindow:
    SecondWindow:
<MainWindow>:
    name: "main"
    GridLayout:
        size_hint: 1, 0.5
        pos_hint: {"y": 0.25}
        cols:1
        GridLayout:
            cols: 2
            Label:
                text: "Password: "
            TextInput:
                id: passw
                multiline: False
                padding : 6,self.height/2 - self.font_size/2,6,6
        Button:
            text: "submit"
            on_release: app.root.current = "second" if passw.text == "password" else "main"
<SecondWindow>:
    name: "second"
    Button:
        size_hint: 0.5, 0.5
        pos_hint: {"x": 0.25, "y": 0.25}
        text: "Go Back"
        on_release: app.root.current = "main"
reference:
Thursday, July 29, 2021
kv 22 simple draw
click or drag and drop to move rectangle
#draw.py
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle
from kivy.graphics import Color
from kivy.graphics import Line
class Draw(Widget):
    def __init__(self, **kwargs):
        super(Draw, self).__init__()
        with self.canvas:
            Color(0, 1, 0, .5, mode='rgba')
            Line(points=(20, 30, 400, 500, 60, 500))
            Color(1, 0, 0, 0.5, mode='rgba')
            self.rect = Rectangle(pos=(100, 100), size=(50, 100))
    def on_touch_down(self, touch):
        self.rect.pos = touch.pos
    def on_touch_move(self, touch):
        self.rect.pos = touch.pos
class DrawApp(App):
    def build(self):
        return Draw()
DrawApp().run()
reference:
Wednesday, July 28, 2021
kv 21 mouse event
#mouse.py
from kivy.app import App
from kivy.uix.widget import Widget
class Touch(Widget):
    def on_touch_down(self, touch):
        print("Mouse Down", touch)
        self.ids["button_id"].opacity = 0.5
    def on_touch_move(self, touch):
        print("Mouse Move", touch)
    def on_touch_up(self, touch):
        print("Mouse Up", touch)
        self.ids["button_id"].opacity = 1
class MouseApp(App):
    def build(self):
        return Touch()
MouseApp().run()
---------------------------\
mouse.kv
<Touch>
    Button:
        id:button_id
        size: root.width-200, root.height-200
        pos: 100, 100
        text:"Button"
---------------
#logs
Mouse Down <MouseMotionEvent spos=(0.35875, 0.5516666666666667) pos=(287.0, 331.00000000000006)>
Mouse Up <MouseMotionEvent spos=(0.35875, 0.5516666666666667) pos=(287.0, 331.00000000000006)>
Mouse Down <MouseMotionEvent spos=(0.30125, 0.32833333333333337) pos=(241.0, 197.00000000000003)>
Mouse Move <MouseMotionEvent spos=(0.305, 0.32999999999999996) pos=(244.0, 197.99999999999997)>
Mouse Move <MouseMotionEvent spos=(0.3075, 0.33166666666666667) pos=(246.0, 199.0)>
reference:
Monday, July 26, 2021
kv 20 floatlayout 1
#float.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()
--------------------
#float.kv
<Button>:
    font_size:40
    color:0.3, 0.6, 0.7, 1
    size_hint: 0.5, 0.5
<Float>:
    FloatLayout:
        size: root.width, root.height
        Button:
            pos_hint: {"x": 0.5, "top": 1}
            text: "Button"
        Button:
            id: btn
            pos_hint: {"y": 0}
            text: "up" if btn.state == "normal" else "down"
            background_color: 0.3, 0.4, 0.5, 1
reference:
Sunday, July 25, 2021
Wednesday, July 21, 2021
kv19 grid in kv
#project structure
grid.kv
gridkv.py
#gridkv.py
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
class Grid(Widget):
    def btn(self):
        print("Name:", self.ids["name_id"].text, "Email:", self.ids["email_id"].text)
class GridApp(App):
    def build(self):
        return Grid()
GridApp().run()
-----------------
#grid.kv
<Grid>
    GridLayout:
        cols: 1
        size: root.width - 200, root.height - 200
        pos: 100, 100
        GridLayout:
            cols:2
            Label:
                text: "Name: "
            TextInput:
                id: name_id
                multiline:False
            Label:
                text: "Email: "
            TextInput:
                id: email_id
                multiline:False
        Button:
            text:"Submit"
            on_press: root.btn()
-----------------
#logs
Name: ab Email: cd
reference:
Tuesday, July 20, 2021
Monday, July 19, 2021
kivy18 grid
#grid.py
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class MyGrid(GridLayout):
    def __init__(self):
        super(MyGrid, self).__init__()
        self.cols = 1
        self.inside = GridLayout()
        self.inside.cols = 2
        self.inside.add_widget(Label(text="First Name: "))
        self.firstname = TextInput(multiline=False)
        self.inside.add_widget(self.firstname)
        self.inside.add_widget(Label(text="Last Name: "))
        self.lastname = TextInput(multiline=False)
        self.inside.add_widget(self.lastname)
        self.inside.add_widget(Label(text="Email: "))
        self.email = TextInput(multiline=False)
        self.inside.add_widget(self.email)
        self.add_widget(self.inside)
        self.submit = Button(text="Submit", font_size=40)
        self.submit.bind(on_press=self.pressed)
        self.add_widget(self.submit)
    def pressed(self, instance):
        name = self.firstname.text
        last = self.lastname.text
        email = self.email.text
        print("First Name:", name, "last Name", last, "email:", email)
class MyApp(App):
    def build(self):
        return MyGrid()
MyApp().run()
reference:
Sunday, July 18, 2021
kivy 17 screen manager
#screen.py
from kivy.app import App
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
import time
import random
class FirstScreen(Screen):
    pass
class SecondScreen(Screen):
    pass
class ColourScreen(Screen):
    colour = ListProperty([1, 0, 0, 1])
class MyScreenManager(ScreenManager):
    def new_colour_screen(self):
        name = str(time.time())
        s = ColourScreen(name=name, colour=[random.random() for _ in range(3)] + [1])
        self.add_widget(s)
        self.current = name
root_widget = Builder.load_string('''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
MyScreenManager:
    transition: FadeTransition()
    FirstScreen:
    SecondScreen:
<FirstScreen>
    name: 'first'
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'first screen'
            font_size: 30
        Image:
            source: 'img/1.jpg'
            allow_stretch: True
            keep_ratio: False
        BoxLayout:
            Button:
                text: 'goto second screen'
                font_size: 30
                on_release: app.root.current = 'second'
            Button:
                text: 'get random colour screen'
                font_size: 30
                on_release: app.root.new_colour_screen()
<SecondScreen>
    name: 'second'
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'second screen'
            font_size: 30
        Image:
            source: 'img/5.jpg'
            allow_stretch: True
            keep_ratio: False
        BoxLayout:
            Button:
                text: 'goto first screen'
                font_size: 30
                on_release: app.root.current = 'first'
            Button:
                text: 'get random colour screen'
                font_size: 30
                on_release: app.root.new_colour_screen()
<ColourScreen>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'colour {:.2},{:.2},{:.2} screen'.format(*root.colour[:3])
            font_size: 30
        Widget:
            canvas:
                Color:
                    rgba: root.colour
                Ellipse:
                    pos: self.pos
                    size: self.size
        BoxLayout:
            Button:
                text: 'goto first screen'
                font_size: 30
                on_release: app.root.current = 'first'
            Button:
                text: 'get random colour screen'
                font_size: 30
                on_release: app.root.new_colour_screen()
''')
class ScreenManagerApp(App):
    def build(self):
        return root_widget
ScreenManagerApp().run()
reference:
Saturday, July 17, 2021
kivy 16 setting 3 .ini
#project structure
setting.py
setting.ini
#setting.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.settings import SettingsWithSidebar
import json
#from kivy.config import ConfigParser
settings_json = json.dumps([
    {'type': 'title', 'title': 'example title'},
    {'type': 'bool', 'title': 'A boolean setting',
     'desc': 'Boolean description text', 'section': 'example',
     'key': 'boolexample'},
    {'type': 'numeric', 'title': 'A numeric setting',
     'desc': 'Numeric description text', 'section': 'example',
     'key': 'numericexample'},
    {'type': 'options', 'title': 'A options setting',
     'desc': 'Options description text', 'section': 'example',
     'key': 'optionsexample', 'options': ['option1', 'option2', 'option3']},
    {'type': 'string', 'title': 'A string setting',
     'desc': 'String description text', 'section': 'example',
     'key': 'stringexample'},
    {'type': 'path', 'title': 'A path setting',
     'desc': 'Path description text', 'section': 'example',
     'key': 'pathexample'},
])
settings_json2 = json.dumps([
    {'type': 'title', 'title': 'example title'},
    {'type': 'bool', 'title': 'A boolean setting',
     'desc': 'Boolean description text', 'section': 'example2',
     'key': 'boolexample'},
    {'type': 'numeric', 'title': 'A numeric setting',
     'desc': 'Numeric description text', 'section': 'example2',
     'key': 'numericexample'},
    {'type': 'options', 'title': 'A options setting',
     'desc': 'Options description text', 'section': 'example2',
     'key': 'optionsexample', 'options': ['option1', 'option2', 'option3']},
    {'type': 'string', 'title': 'A string setting',
     'desc': 'String description text', 'section': 'example2',
     'key': 'stringexample'},
    {'type': 'path', 'title': 'A path setting',
     'desc': 'Path description text', 'section': 'example2',
     'key': 'pathexample'},
])
Builder.load_string('''
<Interface>:
    orientation: 'vertical'
    Button:
        text: 'open the settings!'
        font_size: 150
        on_release: app.open_settings()
''')
class Interface(BoxLayout):
    pass
class SettingsApp(App):
    # config = ConfigParser()
    # config.read('config.ini')
    def build(self):
        self.settings_cls = SettingsWithSidebar
        self.use_kivy_settings = False
        setting = self.config.get('example', 'boolexample')
        print(setting)
        return Interface()
    def build_config(self, config):
        config.setdefaults('example',{
            'boolexample': True,
            'numericexample': 10,
            'optionsexample': 'option2',
            'stringexample': 'some_string',
            'pathexample': ''
        })
        config.setdefaults('example2', {
            'boolexample': True,
            'numericexample': 10,
            'optionsexample': 'option2',
            'stringexample': 'some_string',
            'pathexample': ''
        })
    def build_settings(self, settings):
        settings.add_json_panel('Panel 1', self.config, data=settings_json)
        settings.add_json_panel('Panel 2', self.config, data=settings_json2)
    def on_config_change(self, config, section, key, value):
        print(config, section, key, value)
SettingsApp().run()
-----------------------
setting.ini
[example]
boolexample = 0
numericexample = 100
optionsexample = option1
stringexample = hi
pathexample = C:\Users\zchen
[example2]
boolexample = 0
numericexample = 1111
optionsexample = option2
stringexample = some_string
pathexample = 
reference:
Friday, July 16, 2021
kivy 15 setting 2 change setting
setting.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.settings import SettingsWithSidebar
import json
settings_json = json.dumps([
    {'type': 'title', 'title': 'example title'},
    {'type': 'bool', 'title': 'A boolean setting',
     'desc': 'Boolean description text', 'section': 'example',
     'key': 'boolexample'},
    {'type': 'numeric', 'title': 'A numeric setting',
     'desc': 'Numeric description text', 'section': 'example',
     'key': 'numericexample'},
    {'type': 'options', 'title': 'A options setting',
     'desc': 'Options description text', 'section': 'example',
     'key': 'optionsexample', 'options': ['option1', 'option2', 'option3']},
    {'type': 'string', 'title': 'A string setting',
     'desc': 'String description text', 'section': 'example',
     'key': 'stringexample'},
    {'type': 'path', 'title': 'A path setting',
     'desc': 'Path description text', 'section': 'example',
     'key': 'pathexample'},
])
Builder.load_string('''
<Interface>:
    orientation: 'vertical'
    Button:
        text: 'open the settings!'
        font_size: 150
        on_release: app.open_settings()
''')
class Interface(BoxLayout):
    pass
class SettingsApp(App):
    def build(self):
        self.settings_cls = SettingsWithSidebar
        self.use_kivy_settings = False
        setting = self.config.get('example', 'boolexample')
        print(setting)
        return Interface()
    def build_config(self, config):
        config.setdefaults('example',{
            'boolexample': True,
            'numericexample': 10,
            'optionsexample': 'option2',
            'stringexample': 'some_string',
            'pathexample': ''
        })
    def build_settings(self, settings):
        settings.add_json_panel('Panel Name', self.config, data=settings_json)
    def on_config_change(self, config, section, key, value):
        print(config, section, key, value)
SettingsApp().run()
---------------
#logs
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example boolexample 1
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example boolexample 0
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example numericexample 100
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example optionsexample option3
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example optionsexample option1
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example stringexample hello
<kivy.config.ConfigParser object at 0x0000018DC6333FD0> example stringexample hi
reference:
Thursday, July 15, 2021
kivy 14 settings 1
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.settings import SettingsWithSidebar
import json
settings_json = json.dumps([
    {'type': 'title', 'title': 'example title'},
    {'type': 'bool', 'title': 'A boolean setting',
     'desc': 'Boolean description text', 'section': 'example',
     'key': 'boolexample'},
    {'type': 'numeric', 'title': 'A numeric setting',
     'desc': 'Numeric description text', 'section': 'example',
     'key': 'numericexample'},
    {'type': 'options', 'title': 'A options setting',
     'desc': 'Options description text', 'section': 'example',
     'key': 'optionsexample', 'options': ['option1', 'option2', 'option3']},
    {'type': 'string', 'title': 'A string setting',
     'desc': 'String description text', 'section': 'example',
     'key': 'stringexample'},
    {'type': 'path', 'title': 'A path setting',
     'desc': 'Path description text', 'section': 'example',
     'key': 'pathexample'},
])
Builder.load_string('''
<Interface>:
    orientation: 'vertical'
    Button:
        text: 'open the settings!'
        font_size: 150
        on_release: app.open_settings()
''')
class Interface(BoxLayout):
    pass
class SettingsApp(App):
    def build(self):
        self.settings_cls = SettingsWithSidebar
        return Interface()
    def build_config(self, config):
        config.setdefaults('example',{
            'boolexample': True,
            'numericexample': 10,
            'optionsexample': 'option2',
            'stringexample': 'some_string',
            'pathexample': '/some/path'
        })
    def build_settings(self, settings):
        settings.add_json_panel('Panel Name', self.config, data=settings_json)
SettingsApp().run()
reference:
Tuesday, July 13, 2021
kivy 13 animation
#animation.py
from kivy.animation import Animation
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.properties import ListProperty
from kivy.core.window import Window
from random import random
Builder.load_string('''
<Root>:
    AnimRect:
        pos: 500, 300
<AnimRect>:
    size: 200, 100
    canvas:
        Color:
            rgba: 0, 1, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size
''')
class Root(Widget):
    pass
class AnimRect(Widget):
    def anim_to_random_pos(self):
        Animation.cancel_all(self)
        random_x = random() * (Window.width - self.width)
        random_y = random() * (Window.height - self.height)
        anim = Animation(x=random_x, y=random_y, duration=4, t='out_elastic')
        anim.start(self)
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            self.anim_to_random_pos()
runTouchApp(Root())
reference:
Monday, July 12, 2021
kivy 12 clock
#clock.py
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.properties import ListProperty
from kivy.core.window import Window
Builder.load_string('''
<Root>:
    ClockRect:
        pos: 300, 300
<ClockRect>:
    size: 200, 100
    canvas:
        Color:
            rgba: 1, 0, 0, 1
        Rectangle:
            pos: self.pos
            size: self.size
''')
class Root(Widget):
    pass
class ClockRect(Widget):
    velocity = ListProperty([10, 15])
    def __init__(self, **kargs):
        super(ClockRect, self).__init__(**kargs)
        Clock.schedule_interval(self.update, 1/60)
    def update(self, *args):
        self.x += self.velocity[0]
        self.y += self.velocity[1]
        if self.x < 0 or (self.x + self.width) > Window.width:
            self.velocity[0] *= -1
        if self.y < 0 or (self.y + self.height) > Window.height:
            self.velocity[1] *= -1
runTouchApp(Root())
reference:
Sunday, July 11, 2021
Saturday, July 10, 2021
kivy 11 layout
#layout.py
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.lang import Builder
from kivy.base import runTouchApp
Builder.load_string('''
<RootWidget>:
    Image:
        source: 'img/1.jpg'
        allow_stretch: True
        keep_ratio: False
    Image:
        source: 'img/5.jpg'
        allow_stretch: True
        keep_ratio: False
    Image:
        source: 'img/6.jpg'
        allow_stretch: True
        keep_ratio: False
''')
class RootWidget(Label):
    def do_layout(self, *args):
        number_of_children = len(self.children)
        width = self.width
        width_per_child = int(width / number_of_children)
        positions = range(0, width, width_per_child)
        for position, child in zip(positions, self.children):
            child.height = self.height
            child.x = self.x + position
            child.y = self.y
            child.width = width_per_child
    def on_size(self, *args):
        self.do_layout()
    def on_pos(self, *args):
        self.do_layout()
    def add_widget(self, widget, index=0, canvas=None):
        super(RootWidget, self).add_widget(widget)
        self.do_layout()
    def remove_widget(self, widget):
        super(RootWidget, self).add_widget(widget)
        self.do_layout()
runTouchApp(RootWidget())
reference:
Friday, July 9, 2021
Thursday, July 8, 2021
kivy 10 scrollable label
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.base import runTouchApp
from kivy.lang import Builder
Builder.load_string('''
<ScrollableLabel>:
    text: str('some really long string ' * 100)
    Label:
        text: root.text
        font_size: 50
        text_size: self.width, None
        size_hint_y: None
        height: self.texture_size[1]
''')
class ScrollableLabel(ScrollView):
    text = StringProperty('')
runTouchApp(ScrollableLabel())
reference:
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:
Monday, July 5, 2021
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:
Subscribe to:
Comments (Atom)
 






























