首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kivy按钮背景图像大小

问Kivy按钮背景图像大小
EN

Stack Overflow用户
提问于 2020-12-27 13:21:44
回答 1查看 106关注 0票数 0

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

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

​

​

我的.kv文件

代码语言:javascript
复制
#: 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-28 22:42:12

通过派生新类来创建ImageButton。

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65463089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档