KIVY学习笔记——安卓开发下篇完结

:
    Button:
        text: 'Button 02'

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
class ButtonFloatLayout(FloatLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        bt = Button(text='Button 01')
        self.add_widget(bt)
class ButtonApp(App):
    def build(self):
        return ButtonFloatLayout()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [1,1,1,1]
    ButtonApp().run()

    size_hint: .2, .15
:
    Button:
        text: 'Button 00'
        size_hint: .2, .15
        pos: 65, 400
        background_normal: ''
        background_color: [.1, .5, .5, 1]
    MyButton:
        text: 'Button 01'
        pos: 315, 400
        disabled: True
    MyButton:
        text: 'Button 02'
        color: [.8, .3, .3, 1]
        pos: 565, 400
    MyButton:
        text: 'Button 03'
        font_size: 15
        pos: 65, 150
    MyButton:
        text: 'Button 04'
        font_size: 25
        pos: 315, 150
        state: 'normal'
    MyButton:
        text: 'Button 05'
        pos: 565, 150
        state: 'down'

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
class ButtonFloatLayout(FloatLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class ButtonApp(App):
    def build(self):
        return ButtonFloatLayout()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [1,1,1,1]
    ButtonApp().run()

:
    Button:
        text: 'Button'
        size_hint: .2, .15
        pos_hint: {'center_x':.5, 'center_y':.5}
        background_normal: ''
        background_color: [.1, .5, .5, 1]
        on_release:
            root.release_button()
            print('cocpy.com')
        on_press:
            root.press_button()
            print('c0c.ink')

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
class ButtonFloatLayout(FloatLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    def press_button(self):
        """按下按钮触发事件的回调函数"""
        print('press_button is running')
    def release_button(self):
        """"按下按钮并释放时触发事件的回调函数"""
        print('release_button is running')
class ButtonApp(App):
    def build(self):
        return ButtonFloatLayout()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [1,1,1,1]
    ButtonApp().run()
:
    orientation: 'vertical'
    Label:
        text: 'can only display glyphs that is available in the font'
        font_size: '20sp'
        color: .9, .2, .1, 1
        italic: True
        text_size: cm(6), mm(20)
        halign: 'right'
        valign: 'middle'
        strikethrough: True
    Label:
        canvas:
            Color:
                rgba: (.1, .1, .1, .1)
            Rectangle:
                pos: self.pos
                size: self.size
        text: 'can only display glyphs that is available in the font'
        color: .2, .2, .9, 1
        bold: True
        text_size: cm(4), cm(2)
        halign: 'left'
        valign: 'top'
        underline: True
        line_height: 2.0

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class LabelBoxLayout(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class LabelApp(App):
    def build(self):
        return LabelBoxLayout()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [1,1,1,1]
    LabelApp().run()
:
    orientation: 'vertical'
    Label:
        text: 'can only display [i]glyphs [color=#"000000"]that is available[/color] in the font[/i]'
        font_size: '20sp'
        color: .9, .2, .1, 1
        markup: 1
    Label:
        canvas:
            Color:
                rgba: (.1, .1, .1, .1)
            Rectangle:
                pos: self.pos
                size: self.size
        text: '[b]can only display[/b] glyphs [sup]that[/sup] is available [sub]in[/sub] the font'
        color: .2, .2, .9, 1
        markup: True

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class LabelBoxLayout(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class LabelApp(App):
    def build(self):
        return LabelBoxLayout()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [1,1,1,1]
    LabelApp().run()

:
    orientation: 'vertical'
    Label:
        canvas:
            Color:
                rgba: (.1, .1, .1, .1)
            Rectangle:
                pos: self.pos
                size: self.size
        text: '[ref="click"]can only display[/ref] glyphs that is available in the font'
        color: .2, .2, .9, 1
        markup: True
        on_ref_press:
            print('ref named click is running')
            root.print_it()

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
class LabelBoxLayout(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 设置引用时 markup 属性必须设置为真(True、1等)
        # 将“Label”文本标记,单击“Label”文本时会触发绑定的事件,单击“Hello”文本则不会
        label_ref = Label(text='Hello[ref=label] Label[/ref]', markup=True, color=(.9, .2, .1, 1))
        # 绑定触发事件,回调方法
        label_ref.bind(on_ref_press=self.print_it)
        self.add_widget(label_ref)
    # 未使用到self,建议设置为静态方法
    @staticmethod
    def print_it(*args):
        print('print_it is running')
class LabelApp(App):
    def build(self):
        return LabelBoxLayout()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [1,1,1,1]
    LabelApp().run()
:
    canvas:
        Color:
            rgba: [1, 1, 1, 1]
        Rectangle:
            size: self.width + 20, self.height + 20
            pos: self.x - 10, self.y - 10
            source: 'back.jpg'
    Image:
        source: 'C:/Users/Administrator/Desktop/myback.jpg'
        size_hint_y: None
        width: 100
        allow_stretch: True

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class ImageBoxLayout(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class ImageApp(App):
    def build(self):
        return ImageBoxLayout()
if __name__ == '__main__':
    ImageApp().run()

:
    canvas:
        Color:
            rgba: [1, 1, 1, 1]
        Rectangle:
            size: self.size
            pos: self.pos
            source: 'back.jpg'
    AsyncImage:
        source: 'http://sck.rjkflm.com/images/logo1.png'

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class ImageBoxLayout(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class ImageApp(App):
    def build(self):
        return ImageBoxLayout()
if __name__ == '__main__':
    ImageApp().run()

:
    canvas:
        Color:
            rgba: (1,1,1,1)
        Rectangle:
            pos: self.pos
            size: self.size
    TextInput:
        text: 'c0c.ink'
        multiline: False
        pos: 400, 380
        allow_copy: False
    TextInput:
        text: 'cocpy.com'
        pos: 100, 180
        halign: 'center'
        background_color: 100, 2, 2, 1
    TextInput:
        text: 'first'
        pos: 400, 180
        readonly: True

from kivy.app import App
from kivy.uix.widget import Widget
class TextInputWidget(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class TextInputApp(App):
    def build(self):
        return TextInputWidget()
if __name__ == '__main__':
    TextInputApp().run()

:
    canvas:
        Color:
            rgba: (1,1,1,1)
        Rectangle:
            pos: self.pos
            size: self.size
    TextInput:
        text: 'c0c.ink'
        multiline: False
        pos: 400, 380
        allow_copy: False
        on_text_validate: self.pos=[self.x, self.y-5]
    TextInput:
        text: 'cocpy.com'
        pos: 100, 180
        halign: 'center'
        background_color: 100, 2, 2, 1
        on_text: self.pos=[self.x+5, self.y]
    TextInput:
        text: 'first'
        pos: 400, 180
        readonly: True
        on_double_tap: self.pos=[self.x-5, self.y]
        on_triple_tap: self.pos=[self.x, self.y+5]
        on_quad_touch: self.pos=[400, 180]

from kivy.app import App
from kivy.uix.widget import Widget
class TextInputWidget(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class TextInputApp(App):
    def build(self):
        return TextInputWidget()
if __name__ == '__main__':
    TextInputApp().run()

:
    orientation: 'vertical'
    CheckBox:
        group: 'check_0'
        id: first_check_0
        color: .6, .2, .2, 1
    CheckBox:
        group: 'check_0'
        color: .2, .6, .2, 1
    CheckBox:
        group: 'check_0'
        color: .2, .2, .6, 1
    CheckBox:
        color: .6, .6, .6, 1

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class CheckBoxWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class CheckBoxApp(App):
    def build(self):
        return CheckBoxWidget()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    CheckBoxApp().run()

:
    orientation: 'vertical'
    CheckBox:
        group: 'check_0'
        id: first_check_0
        color: .6, .2, .2, 1
    CheckBox:
        group: 'check_0'
        color: .2, .6, .2, 1
    CheckBox:
        group: 'check_0'
        color: .2, .2, .6, 1
    CheckBox:
        color: .6, .6, .6, 1

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class CheckBoxWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 通过id获取到CheckBox部件并绑定方法
        self.ids.first_check_0.bind(active=self.on_checkbox_active)
    @staticmethod
    def on_checkbox_active(checkbox, value):
        if value:
            print('The checkbox', checkbox, 'is active')
        else:
            print('The checkbox', checkbox, 'is inactive')
class CheckBoxApp(App):
    def build(self):
        return CheckBoxWidget()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    CheckBoxApp().run()
:
    orientation: 'vertical'
    Slider:
        min: 0
        max: 100
    Slider:
        id: slider_move
        orientation: 'vertical'
        min: -100
        max: 100
        step: 10
        value_track: True
        value_track_color: [1, 0, 0, 1]
        value: 25
        pos: 200, 400

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class SliderWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class SliderApp(App):
    def build(self):
        return SliderWidget()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    SliderApp().run()
:
    Slider:
        min: 0
        max: 100
        on_touch_down: print('down')
        on_touch_up: print('up')
        on_touch_move: print('move')

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class SliderWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class SliderApp(App):
    def build(self):
        return SliderWidget()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    SliderApp().run()

:
    ProgressBar:
        value: 30
        id: progress_bar
        max: 100
        on_touch_down: root.clicked()

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
class ProgressBarWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.i = 0
    def clicked(self):
        # 每0.5s调用update_bar方法一次
        self.update_bar_trigger = Clock.schedule_interval(self.update_bar, 0.5)
    def update_bar(self, dt):
        if self.i <= 100:
            # 赋值
            self.ids.progress_bar.value += self.i
            self.i += 1
            self.update_bar_trigger()
class ProgressBarApp(App):
    def build(self):
        return ProgressBarWidget()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    ProgressBarApp().run()

:
    orientation: 'vertical'
    ToggleButton:
        text: 'Male'
        group: 'sex'
        on_release: print('You have clicked Male Button')
    ToggleButton:
        text: 'Female'
        group: 'sex'
        state: 'down'

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.togglebutton import ToggleButton
class ToggleButtonWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        tb = ToggleButton(text='Mixed', group='sex')
        self.add_widget(tb)
class ToggleButtonApp(App):
    def build(self):
        return ToggleButtonWidget()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    ToggleButtonApp().run()
:
    Switch:
        id: switch
        on_active: root.callback()

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class SwitchWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    def callback(self):
        print('Callback is running')
class SwitchApp(App):
    def build(self):
        return SwitchWidget()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    SwitchApp().run()
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.video import Video
class VideoWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        def on_position_change(instance, value):
            print('The position in the video is', value)
        def on_duration_change(instance, value):
            print('The duration of the video is', value)
        video = Video(source='34.mp4', state='play')
        video.bind(position=on_position_change, duration=on_duration_change)
        self.add_widget(video)
class VideoApp(App):
    def build(self):
        return VideoWidget()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    VideoApp().run()
:
    size_hint: .5, .2
    pos_hint: {'center_x': .5, 'center_y': .5}
    Bubble:
        arrow_pos: 'bottom_left'
    Bubble:
        Button:
            text: 'Bubble'
            on_release: print('Bubble Button ')

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class BubbleWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class BubbleApp(App):
    def build(self):
        return BubbleWidget()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    BubbleApp().run()
:
    size_hint: .5, .2
    pos_hint: {'center_x': .5, 'center_y': .5}
    Bubble:
        BubbleButton:
            text: 'Bubble'
        BubbleButton:
            text: 'Button'
            background_normal: ''
            background_color: .1, .5, .5, 1
            on_release: print('Bubble Button ')

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class BubbleWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class BubbleApp(App):
    def build(self):
        return BubbleWidget()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    BubbleApp().run()

:
    Button:
        text: 'Item0'
        size_hint_y: None
        height: 44
        on_release: root.select('item0')
    Button:
        text: 'item1'
        size_hint_y: None
        height: 44
        on_release: root.select('item1')
    Button:
        text: 'Item2'
        size_hint_y: None
        height: 44
        on_release: root.select('item2')

from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.base import runTouchApp
class CustomDropDown(DropDown):
    """在kv文件中添加下拉选项"""
    pass
class DropDownBox(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        dropdown = CustomDropDown()
        # 点击该按钮触发下拉框
        main_button = Button(text='SelectItem', size_hint=(0.2, 0.15), pos_hint={'center_x':0.5, 'center_y':0.5})
        main_button.bind(on_release=dropdown.open)
        # 绑定选中后回调的方法:把 main_button 的 text 属性设置为传递过来的 x
        dropdown.bind(on_select=lambda instance, x: setattr(main_button, 'text', x))
        # self.add_widget(main_button)
        # 创建一个MTWindow并将窗口小部件作为根窗口小部件添加到窗口中
        runTouchApp(main_button)
class DropDownApp(App):
    def build(self):
        return DropDownBox()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    DropDownApp().run()

#: import Factory kivy.factory.Factory
:
    auto_dismiss: False
    title: 'Hello Popup'
    on_dismiss: print('on_dismiss is running')
    on_open: print('on_open is running')
    size_hint: .8, .8
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        Button:
            text: 'Close Popup!'
            size_hint: None, None
            size: 100, 60
            on_release: root.dismiss()
:
    Button:
        text: 'Open popup'
        size: 100, 120
        on_release: Factory.MyPopup().open()

from kivy.app import App
from kivy.uix.widget import Widget
class PopupBox(Widget):
    def __int__(self, **kwargs):
        super().__int__(**kwargs)
class PopupApp(App):
    def build(self):
        return PopupBox()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    PopupApp().run()
:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser
        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()
            Button:
                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)
:
    Button:
        text:"choose file"
        size_hint: .2, .1
        on_release: root.show_load()

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class MyFileChooser(BoxLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)
class FileChooserBox(BoxLayout):
    loadfile = ObjectProperty(None)
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    def show_load(self):
        content = MyFileChooser(load=self.load, cancel=self.dismiss_popup)
        # 打开一个弹窗
        self._popup = Popup(title="Load file", content=content, size_hint=(0.9, 0.9))
        self._popup.open()
    def load(self, path, filename):
        print(path, filename)
        self.dismiss_popup()
    def dismiss_popup(self):
        # 关闭弹窗
        self._popup.dismiss()
class FileChooserApp(App):
    def build(self):
        return FileChooserBox()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8, .8, .8, 1]
    FileChooserApp().run()
:
    Spinner:
        size_hint: None, None
        size: 100, 44
        pos_hint: {'center': (.5, .5)}
        text: 'Home'
        values: 'Home', 'Work', 'Other', 'Custom'
        on_text: print("The spinner's text is: ", self.text)

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class SpinnerBox(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class SpinnerApp(App):
    def build(self):
        return SpinnerBox()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    SpinnerApp().run()

:
    viewclass: 'Label'
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'

from kivy.app import App
from kivy.uix.recycleview import RecycleView
class RecycleViewWidget(RecycleView):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(100)]
class RecycleViewApp(App):
    def build(self):
        return RecycleViewWidget()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    RecycleViewApp().run()

:
    size_hint: .5, .5
    pos_hint: {'center_x': .5, 'center_y': .5}
    do_default_tab: False
    TabbedPanelItem:
        text: 'first tab'
        Label:
            text: 'First tab content area'
    TabbedPanelItem:
        text: 'tab2'
        BoxLayout:
            Label:
                text: 'Second tab content area'
            Button:
                text: 'Button that does nothing'
    TabbedPanelItem:
        text: 'tab3'
        RstDocument:
            text:
                '\\n'.join(("Hello world", "-----------",
                "You are in the third tab."))

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
class TabbedPanelTest(TabbedPanel):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
class TabbedPanelApp(App):
    def build(self):
        return TabbedPanelTest()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    TabbedPanelApp().run()

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.videoplayer import VideoPlayer
class VideoPlayerTest(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        player = VideoPlayer(source='demo0.mp4', state='play',
                             options={'allow_stretch': True, 'eos': 'loop'})
        self.add_widget(player)
class VideoPlayerApp(App):
    def build(self):
        return VideoPlayerTest()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    VideoPlayerApp().run()

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.vkeyboard import VKeyboard
class VKeyboardTest(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        vk = VKeyboard()
        vk.bind(on_key_up=self.key_up)
        self.add_widget(vk)
    def key_up(self, *args):
        print('You have pressed the key is:',args[2])
class VKeyboardApp(App):
    def build(self):
        return VKeyboardTest()
if __name__ == '__main__':
    from kivy.core.window import Window
    Window.clearcolor = [.8,.8,.8,1]
    VKeyboardApp().run()

 

你可能感兴趣的:(2020年暑假研零笔记)