【问题标题】:In kivy, why does size_hint behave differently for images than for buttons?在 kivy 中,为什么 size_hint 对图像的行为与对按钮的行为不同?
【发布时间】:2016-07-23 11:46:26
【问题描述】:

我正在使用 kivy 1.9.1 构建一个纸牌游戏。我通过 size_hint 让卡片正确显示,相对于根窗口的大小。卡片类继承自图像类并且工作得很好。我意识到我需要使卡片可点击,所以我修改了类以继承自按钮类。由于某种原因,这与图像的大小不同。背景 .png 文件变形。请帮忙。这让我发疯了。我通常关闭 size_hint 以避免这个问题,但我需要根据根窗口大小缩放所有内容。

ScreenManagement:
    CardTableScreen:
<Card>:
    size_hint: (.25, .25)
    pos_hint: ({'left': .05})
<CardTableScreen>:
    name: 'cardTable'

    Card:
        name: 'card0'
        id: card0
        pos: (self.width *.20 , root.height / 2)
    Card:
        name: 'card1'
        id: card1
        pos: (self.width * .75, root.height / 2)
    Card:
        name: 'card2'
        id: card2
        pos: (self.width * 1.30 , root.height / 2)
    Card:
        name: 'card3'
        id: card3
        pos: (self.width * 1.85, root.height / 2)
    Card:
        name: 'card4'
        id: card4
        pos: (self.width * 2.40, root.height / 2)
    Label:
        name: 'handType'
        id: handType
        pos: (-(card0.width *.125), root.height * .30)
        font_size: '18sp'

<Layout>:
    orientation: 'vertical'
    canvas.before:
        Color:
            rgba: 0,.25,0,1
        Rectangle:
            pos: self.pos
            size: self.size

蟒蛇:

from kivy.uix.button import Button
class(Button): pass

【问题讨论】:

标签: python image button scale kivy


【解决方案1】:

原来,解决办法是从图片类和按钮行为类继承,像这样:

从 kivy.uix.behaviors 导入 ButtonBehavior
从 kivy.uix.image 导入图片

类卡片(按钮行为,图像):通过

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2016-02-03
    • 1970-01-01
    • 2021-10-24
    • 2019-08-19
    相关资源
    最近更新 更多