samedi 18 juin 2016

size_hint and pos_hint do nothing when creating widget with python code

I am unable to resize a widget when using this code:

class GameScreen(Screen):

def DrawCard(self):
    CardToDraw=Player1Hand[0]

    if CardToDraw == 'Card1':
        GameScreen.Card1(self)

def Card1(self):

    f = FloatLayout(size_hint=(0.1, 0.2), pos_hint={'center_x': .5, 'y': 0})
    s = Scatter()

    f.add_widget(s)
    s.add_widget(Card1Img)
    self.add_widget(f)

And my kivy code related to that class:

<GameScreen>:
name: 'GameScreen'
on_enter: root.DrawCard()
canvas:
    Rectangle:
        pos: self.pos
        size: self.size
        source: './Images/RetailBoxArtTop.png'
FloatLayout:
    name: 'PlayerHandArea'
    pos_hint: {'center_x': .5, 'y': 0}
    size_hint: 0.6, 0.2     
    canvas:
        Color:
            rgba:1,0,0,0.5
        Rectangle:
            pos: self.pos
            size: self.size

Player1Hand is just a list variable, and Card1Img is an Image variable. While I don't get any errors with this, it doesn't resize or position my widget. The default position and size is still being used. Preferably, I would like to make each card a class so I can just write this widget out in kivy code but I don't know how to do that. When I tried running the card as a class with say, Card1(FloatLayout) instead of GameScreen.Card1(self), it would do nothing even though I built the widget with kivy code. I also don't like using the on_enter command to run the function as this seems like it could cause problems if someone switched to a options menu and came back to the GameScreen, but I dont know how to run it right away without that. If anyone knows how to do that, I would love to know, but an answer to either question would be awesome!

Aucun commentaire:

Enregistrer un commentaire