python篇-kivy
1,1-06_size_hint_demo
from kivy.app import App class WeatherApp(App): pass if __name__ == '__main__': WeatherApp().run()
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
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()
<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
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)
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()
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
5,