Kivy按钮背景图像大小

Kivy button background image size

我是 Kivy 的新手,我正在尝试使用 kivy 和 scrollview。 我需要停止按钮背景图片的拉伸,我希望按钮看起来像第一张图片。我正在使用 background_normal 将背景图像添加到按钮。我需要更改网格布局中的 row_default_height: root.height*0.3 吗?还是在每个按钮上添加图像高度和宽度?如何阻止图像调整大小?

任何帮助都会很好,谢谢 :)

我的 .kv 文件

#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import GridLayout kivy.uix.gridlayout
#: import BoxLayout kivy.uix.boxlayout
#: import ButtonBehavior kivy.uix.button
#: import Image kivy.uix.image
#: import Window kivy.core.window.Window
ScreenManager:
    transition: FadeTransition()
    MainScreen:
    AnotherScreen:

<MainScreen>:
    name: "main"
    BoxLayout:
        ScrollView:
            GridLayout:
                some_property: setattr(Window, 'fullscreen' , 'auto')  or 'real_value!'
                id: container_y
                size_hint_y: None
                cols: 2
                row_default_height: root.height*0.3
                height: self.minimum_height
                Image:
                    source: "teaflav/Crushes.png"
                Button:
                    background_normal: 'teaflav/Crushes.png'
                    on_release: app.root.current ="other"
                    height: 40
                Button:
                    background_normal: 'teaflav/Crushes.png'
                    on_release: app.root.current ="other"
                    height: 40
                Button:
                    background_normal: 'teaflav/Crushes.png'
                    on_release: app.root.current ="other"
                    height: 40
                Button:
                    background_normal: 'teaflav/Crushes.png'
                    on_release: app.root.current ="other"
                    height: 40
                Button:
                    background_normal: 'teaflav/Crushes.png'
                    on_release: app.root.current ="other"
                    height: 40

通过派生新的 class 创建一个 ImageButton。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image


kv = """
#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManager:
    transition: FadeTransition()
    MainScreen:
        name: "main"
    AnotherScreen:
        name: 'other'

<AnotherScreen@Screen>:
    Button:
        text: 'Return to Main'
        on_release: root.manager.current = 'main' 
        

<MainScreen@Screen>:
    BoxLayout:
        ScrollView:
            GridLayout:
                id: container_y
                size_hint_y: None
                cols: 2
                row_default_height: root.height*0.3
                height: self.minimum_height
                Image:
                    source: "drink.png"
                ImageButton:
                    source: 'drink.png'
                    on_release: app.root.current ="other"
                    size_hint_y: None
                    height: 40
                ImageButton:
                    source: 'drink.png'
                    on_release: app.root.current ="other"
                    size_hint_y: None
                    height: 40
                ImageButton:
                    source: 'drink.png'
                    on_release: app.root.current ="other"
                    size_hint_y: None
                    height: 40
                ImageButton:
                    source: 'drink.png'
                    on_release: app.root.current ="other"
                    size_hint_y: None
                    height: 40
                ImageButton:
                    source: 'drink.png'
                    on_release: app.root.current ="other"
                    size_hint_y: None
                    height: 40
"""


class ImageButton(ButtonBehavior, Image):
    pass


class ImageButtonApp(App):
    def build(self):
        return Builder.load_string(kv)


ImageButtonApp().run()