简体   繁体   English

在kivy中,为什么size_hint的图像行为不同于按钮的行为?

[英]In kivy, why does size_hint behave differently for images than for buttons?

I'm building a card game using kivy 1.9.1. 我正在使用Kivy 1.9.1构建纸牌游戏。 I Had the cards displaying correctly, relative to the size of the root window, via size_hint. 我已经通过size_hint相对于根窗口的大小正确显示了卡片。 The cards class was inheriting from the image class and worked great. 卡类从图像类继承而来,并且效果很好。 I realized that I needed to make the cards clickable, so I modified the class to inherit from the button class instead. 我意识到我需要使卡片可点击,因此我修改了该类以从按钮类继承。 For some reason, this did not size the same as an image. 由于某种原因,它的大小与图像的大小不同。 The background .png file became distorted. 背景.png文件变形。 Please help. 请帮忙。 This is driving me nuts. 这让我发疯。 I generally turn off size_hint to avoid this issue, but I need everything scaled based on root window size. 我通常会关闭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

python: 蟒蛇:

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

Turns out, the solution is to inherit from the image class and the button behavior class, like so: 事实证明,解决方案是从图像类和按钮行为类继承,如下所示:

from kivy.uix.behaviors import ButtonBehavior 从kivy.uix.behaviors导入ButtonBehavior
from kivy.uix.image import Image 从kivy.uix.image导入图像

class Cards(ButtonBehavior, Image): pass 类Cards(ButtonBehavior,Image):通过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM