我想编写一个类似画廊的程序,其中动态显示图片,即,我将一些图像源放入列表中,然后进入另一个屏幕查看它们。这是下面的相关代码:
from kivy.uix.stacklayout import StackLayout
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.image import Image
Builder.load_string(
"""
<GalleryScreen>:
BoxLayout:
Button:
text: "insert picture 2"
on_press: app.insert_pic2()
Button:
text: "goto screen"
on_press: root.manager.current = "stack"
<StackScreen>:
BoxLayout:
DisplayStack:
Button:
text: "go back"
on_press: root.manager.current = "gallery"
"""
)
class GalleryScreen(Screen):
pass
class StackScreen(Screen):
pass
class DisplayStack(StackLayout):
def __init__(self, **kwargs):
super(DisplayStack, self).__init__(**kwargs)
for i in self.pics:
surse = i
st = Image(size_hint=(.2, .2), source=surse)
self.add_widget(st)
class GalleryApp(App):
pics = ['Pictures/1.png']
def insert_pic2(self):
self.pics.append("Pictures/2.jpg")
def build(self):
sm = ScreenManager()
sm.add_widget(GalleryScreen(name='gallery'))
sm.add_widget(StackScreen(name='stack'))
return sm
if __name__ == '__main__':
GalleryApp().run()
要点是我想从 GalleryScreen 到 StackScreen 并查看来源在“图片”列表中的图片,然后返回,在列表中插入新的图像源,然后再次进入 StackScreen 并查看新的整个列表。