Kivy:不知道如何在ScreenManager中更新“ on_size:root.center = win.Window.center”

时间:2019-07-05 16:12:25

标签: kivy kivy-language

我将Kivy散布示例添加到kivy屏幕中。但是它不能正常工作。我必须在窗口上重新配置中心。这是在kv文件中完成的。但我不知道如何在屏幕上进行操作。请参见下面的代码。

python

class Picture(Scatter):

    source = StringProperty(None)


class ScreenThree(Screen):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        root = BoxLayout()    # instantiate BoxLayout
        self.add_widget(root)    # add BoxLayout to screen      


        curdir = dirname(__file__)

        for filename in glob(join(curdir, 'images', '*')):
            print(filename)
            try:
                picture = Picture(source=filename, rotation=randint(-30, 25))

                root.add_widget(picture)


            except Exception as e:
                Logger.exception('Pictures: Unable to load <%s>' % filename)                



    def on_pause(self):
        return True             

class TestApp(App):

    def build(self):


        sm = ScreenManager()

        sc1 = ScreenOne(name='screen1')
        sc2 = ScreenTwo(name='screen2')        
        sc3 = ScreenThree(name='screen3')   

        sm.add_widget(sc1)
        sm.add_widget(sc2)
        sm.add_widget(sc3)

        print (sm.screen_names)

        return sm        

if __name__ == '__main__':
    TestApp().run()

猕猴桃

#:kivy 1.0
#:import kivy kivy
#:import win kivy.core.window

FloatLayout:
    canvas:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            source: 'data/images/background.jpg'
            size: root.size

    BoxLayout:
        padding: 10
        spacing: 10
        size_hint: 1, None
        pos_hint: {'top': 1}
        height: 44
        Image:
            size_hint: None, None
            size: 24, 24
            source: 'data/logo/kivy-icon-24.png'
        Label:
            height: 24
            text_size: self.width, None
            color: (1, 1, 1, .8)
            text: 'Kivy %s - Pictures' % kivy.__version__



<Picture>:

    on_size: root.center = win.Window.center     <-- this is the question i guess
    size: image.size
    size_hint: None, None

    Image:
        id: image
        source: root.source

        # create initial image to be 400 pixels width
        size: 400, 400 / self.image_ratio

        # add shadow background
        canvas.before:
            Color:
                rgba: 1,1,1,1
            BorderImage:
                source: 'shadow32.png'
                border: (36,36,36,36)
                size:(self.width+72, self.height+72)
                pos: (-36,-36)

在此处查看示例,Kivy Gallery of Examples » Basic Picture Viewer

1 个答案:

答案 0 :(得分:0)

在kv文件中,当您创建实例化对象on_size: root.center = win.Window.center作为类规则的子级FloatLayout:以及Python脚本中的一些增强功能时,<ScreenThree>:可以正常工作。

kv文件

  • 用安装的Kivy版本替换#:kivy 1.0,例如#:kivy 1.11.1
  • 添加类规则<ScreenThree>:,并使FloatLayout:对象成为<ScreenThree>:的子对象

摘要-kv文件

<ScreenThree>:
    FloatLayout:
        canvas:
            Color:
                rgb: 1, 1, 1
            Rectangle:
                source: 'data/images/background.jpg'
                size: root.size

        BoxLayout:
            padding: 10
            spacing: 10
            size_hint: 1, None
            pos_hint: {'top': 1}
            height: 44
            Image:
                size_hint: None, None
                size: 24, 24
                source: 'data/logo/kivy-icon-24.png'
            Label:
                height: 24
                text_size: self.width, None
                color: (1, 1, 1, .8)
                text: 'Kivy %s - Pictures' % kivy.__version__

py文件

  • 删除root = BoxLayout()self.add_widget(root)
  • root.add_widget(picture)替换为self.add_widget(picture)

代码段-py文件

class ScreenThree(Screen):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        curdir = dirname(__file__)

        for filename in glob(join(curdir, 'images', '*')):
            print(filename)
            try:
                picture = Picture(source=filename, rotation=randint(-30, 25))
                self.add_widget(picture)
            except Exception as e:
                Logger.exception('Pictures: Unable to load <%s>' % filename)

输出

Pictures - Moved