python篇-kivy

1,1-06_size_hint_demo

    

from kivy.app import App


class WeatherApp(App):
    pass

if __name__ == '__main__':
    WeatherApp().run()
main.py
BoxLayout:
    orientation: "vertical"
    BoxLayout:
        Button:
            size_hint_x: 1
        Button:
            size_hint_x: 1
        Button:
            size_hint_x: 1
    BoxLayout:
        Button:
            size_hint_x: 1
        Button:
            size_hint_x: 2
        Button:
            size_hint_x: 3
    BoxLayout:
        Button:
            size_hint_x: 1
        Button:
            size_hint_x: 0.75
        Button:
            size_hint_x: 0.25
weather.kv

 

2,   滑动屏幕

  

 

 

from kivy.app import App
from kivy.uix.recycleview import RecycleView


class ExampleViewer(RecycleView):

    def __init__(self, **kwargs):
        super(ExampleViewer, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(20)]
        print('date=', self.data)


class SampleApp(App):
    def build(self):
        return ExampleViewer()


SampleApp().run()
main.py
<ExampleViewer>:
     viewclass: 'Button'  # defines the viewtype for the data items.
     orientation: "vertical"
     spacing: 40
     padding: 10,10
     space_x: self.size[0]/3
     RecycleBoxLayout:
          color: '#008000'
          default_size: None , dp(56)
          # defines the size of the widget in reference to width and height
          default_size_hint: 0.4 , None
          size_hint_y: None
          height: self.minimum_height
          orientation: 'vertical' # defines the orientation of data items
sample.py

 

3,下拉框

        

 

from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button

from kivy.base import runTouchApp

dropdown = DropDown()

for index in range(10):
    btn = Button(text ='Value % d' % index, size_hint_y = None, height = 40)
    btn.bind(on_release = lambda btn: dropdown.select(btn.text))
    dropdown.add_widget(btn)

mainbutton = Button(text ='Hello', size_hint =(None, None), pos =(350, 300))
mainbutton.bind(on_release = dropdown.open)
dropdown.bind(on_select = lambda instance, x: setattr(mainbutton, 'text', x))

runTouchApp(mainbutton)
View Code

 

4,  点击按钮,屏幕切换

py文件

from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition

from kivy.app import App


class MainScreen(Screen):
    pass

class AnotherScreen(Screen):
    pass

class ScreenMangerment(ScreenManager):
    pass

class MainApp(App):

    def build(self):
        return Builder.load_file('kvtest.kv')

if __name__ == '__main__':
    MainApp().run()
kvtest.py

kv文件

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenMangerment:
    transition:FadeTransition()
    MainScreen:
    AnotherScreen:

<MainScreen>
    name:"main"
    Button:
        on_release: app.root.current = "other"
        text:'next screen'
        font_size:50

<AnotherScreen>
    name:"other"
    Button:
        on_release: app.root.current = "main"
        text:'back home'
        font_size:50
kvtest.kv

 

5,

 

posted @ 2022-04-11 16:39  夜未央leo  阅读(213)  评论(0编辑  收藏  举报