简体   繁体   中英

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

I'm building a card game using kivy 1.9.1. I Had the cards displaying correctly, relative to the size of the root window, via 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. 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.

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
from kivy.uix.image import Image

class Cards(ButtonBehavior, Image): pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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