【问题标题】:Kivy button background image sizeKivy 按钮背景图像大小
【发布时间】:2021-04-04 08:49:22
【问题描述】:

我是 Kivy 的新手,我正在尝试使用 kivy 和滚动视图。 我需要停止按钮背景图像的拉伸,我希望按钮看起来像第一张图像。我正在使用 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

【问题讨论】:

    标签: python kivy scrollview


    【解决方案1】:

    通过派生一个新类来创建一个 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()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多