在 kivy 中用动作交换屏幕
Swapping screens with motion in kivy
是否可以通过动作而不是按下按钮来切换屏幕?我在移动应用程序的上下文中询问,用户通常期望这种行为。
您可以使用 Carousel 小部件。例如:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_string("""
<MyWidget>:
Carousel:
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 1"
Label:
text: "Some text"
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 2"
BoxLayout:
Button:
text: "1"
Button:
text: "2"
Button:
text: "3"
Button:
text: "4"
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 3"
Label:
text: "Some other text"
""")
class MyWidget(BoxLayout):
pass
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
我认为这可能是更好的做事方式:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.carousel import Carousel
from kivy.lang import Builder
Builder.load_string("""
<MyCarousel>:
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 1"
Label:
text: "Some text"
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 2"
BoxLayout:
Button:
text: "1"
Button:
text: "2"
Button:
text: "3"
Button:
text: "4"
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 3"
Label:
text: "Some other text"
""")
class MyCarousel(Carousel):
pass
class MyApp(App):
def build(self):
return MyCarousel()
if __name__ == '__main__':
MyApp().run()
显然,Carousel 的行为与其他小部件不同,因为它没有默认大小 100、100。
是否可以通过动作而不是按下按钮来切换屏幕?我在移动应用程序的上下文中询问,用户通常期望这种行为。
您可以使用 Carousel 小部件。例如:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_string("""
<MyWidget>:
Carousel:
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 1"
Label:
text: "Some text"
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 2"
BoxLayout:
Button:
text: "1"
Button:
text: "2"
Button:
text: "3"
Button:
text: "4"
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 3"
Label:
text: "Some other text"
""")
class MyWidget(BoxLayout):
pass
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
我认为这可能是更好的做事方式:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.carousel import Carousel
from kivy.lang import Builder
Builder.load_string("""
<MyCarousel>:
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 1"
Label:
text: "Some text"
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 2"
BoxLayout:
Button:
text: "1"
Button:
text: "2"
Button:
text: "3"
Button:
text: "4"
BoxLayout:
orientation: 'vertical'
Label:
text: "Screen 3"
Label:
text: "Some other text"
""")
class MyCarousel(Carousel):
pass
class MyApp(App):
def build(self):
return MyCarousel()
if __name__ == '__main__':
MyApp().run()
显然,Carousel 的行为与其他小部件不同,因为它没有默认大小 100、100。