【问题标题】:How can I have a fixed width between images in Kivy while adjusting the window?调整窗口时如何在 Kivy 中的图像之间设置固定宽度?
【发布时间】:2019-12-27 16:54:12
【问题描述】:

我有一些图像,大小均为 680 x 1000 像素。我在 Kivy 中设置了一个水平滚动视图(使用 python),我想在其中显示这些图像。问题是当我调整窗口大小时,图像之间的空间会发生变化。到目前为止,这是我的代码:

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image

class MyGrid(GridLayout):
    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        self.rows = 1
        self.size_hint = None, .8
        self.bind(minimum_width=self.setter('width'))    

class MyBox(BoxLayout):
    def __init__(self, **kwargs):
        super(MyBox, self).__init__(**kwargs)
        self.orientation = "vertical"
        self.size_hint_x = None
        self.bind(minimum_width=self.setter('width'))    

class ScrollViewApp(App):
    def build(self):
        base = FloatLayout()
        grid = MyGrid()
        grid.width = 680
        box = MyBox()
        box.add_widget(Widget(size_hint_y=.1))
        box.add_widget(grid)
        box.add_widget(Widget(size_hint_y=.1))

        img = Image(source="img.jpg", allow_stretch=True, keep_ratio=True)
        img.stretch = True
        img.size = grid.size
        grid.add_widget(img)
        img = Image(source="img2.jpg", allow_stretch=True, keep_ratio=True)
        img.stretch = True
        img.size = grid.size
        grid.add_widget(img)

        scroll = ScrollView(do_scroll_y=False, pos_hint={"center_y": .5})
        scroll.add_widget(box)
        base.add_widget(scroll)
        return base    

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

我发现这篇文章很有用,虽然它似乎没有解决我需要的所有问题:kivy image button size and position

如果我在 kivy 中拥有最大尺寸的图像,我会得到以下信息:

这些图片靠得太近了!在另一端,如果我缩小窗口,我会得到:

现在他们离得太远了!我怎样才能做到这一点,以便随着图像尺寸的增大或缩小,它们彼此之间保持相同的水平距离?即使我在 MyGrid() 类中添加填充,我仍然有同样的问题。

【问题讨论】:

    标签: python python-3.x kivy


    【解决方案1】:

    我更喜欢让Kivy 为我做绑定,所以我的解决方案使用kv

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.image import Image
    
    
    class MyImage(Image):
        pass
    
    
    theRoot = Builder.load_string('''
    ScrollView:
        do_scroll_y: False
        BoxLayout:
            id: box
            orientation: 'horizontal'
            spacing: 10
            size_hint: None, 1.0
            width: self.minimum_width    # binds BoxLayout width to the sum of child widths (plus spacing, padding, etc)
    <MyImage>:
        size_hint: None, 1.0
        allow_stretch: True
        keep_ratio: True
        width: self.height * self.image_ratio    # set width (needed for BoxLayout minimum width calculations)
    ''')
    
    
    class ScrollViewApp(App):
        def build(self):
            box = theRoot.ids.box
            img = MyImage(source="img.jpg")
            box.add_widget(img)
            img = MyImage(source="img2.jpg")
            box.add_widget(img)
            return theRoot
    
    if __name__ == '__main__':
        ScrollViewApp().run()
    

    【讨论】:

    • 感谢您的回复!而不是使用 Builder.load_string,我将如何将此代码放入 .kv 文件中?
    • 您可以将kv 字符串放入名为scrollview.kv 的文件中(以便自动加载),或者您可以将其命名为其他名称并使用Builder.load_file()
    • 非常感谢,您的回复(和解决方案)解决了我的问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 2014-01-22
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    相关资源
    最近更新 更多