Gilgamesch
Gilgamesch

Reputation: 311

Kivy widget is bigger than it should be

I tried to make by own coockie-clicker, so I creaded an kivy widget and declared an image of an coockie as part of it. Everytime you click on the wiget, a counter goes up and the number is displayed on an label. Everything went fine, after I got help here on stack overflow, but now I am faced with the problem, that the widet is to big, so even if I click on the right upper corner, to counter goes up, aldoug I do not clicked on the coockie. Here is the sourcecode:

from kivy.app import App
from kivy.lang import Builder

from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.core.window import Window

from kivy.clock import Clock
from kivy.animation import Animation
from  kivy.core.text.markup import *

from kivy.uix.floatlayout import FloatLayout
from kivy.properties import NumericProperty
from kivy.properties import StringProperty

Builder.load_string('''
<Root>:

    Kecks:
        pos: 300, 300
        size: 100, 100
<Kecks>:

    Image:
        pos: root.pos
        id: my_image
        source: root.weg

    Label:
        id: my_Label
        font_size: 50
        text: root.txt
        center_x: 345
        center_y: 200       
''')

class Root(FloatLayout):
    def __init__(self, *args, **kwargs):
        super(Root, self).__init__(*args, **kwargs)

class Kecks(Widget):

    count = NumericProperty(0)
    amount = NumericProperty(1)
    txt = StringProperty()
    level = NumericProperty(1)
    weg = StringProperty('piernik.png')

    def __init__(self, *args, **kwargs):
        super(Kecks, self).__init__(*args, **kwargs)
        #self.txt = str(self.count)
        Clock.schedule_interval(self.Update, 1/60.)

    def Update(self, *args):
        self.txt = str(self.count)
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            self.count += self.amount


class app(App):
    def build(self):
        Window.clearcolor = (10, 0, 0, 1)
        return Root()

if __name__ == "__main__":
    app().run()

Upvotes: 0

Views: 137

Answers (1)

Anzel
Anzel

Reputation: 20583

The problem is you haven't defined your collide_points on which area you want that event to be triggered.

Consider if you want your collide_points on your my_image to trigger on_touch_down event, you need to adjust like this:

def on_touch_down(self, touch):
    # to check if touch.pos collides on my_image 
    if self.ids.my_image.collide_point(*touch.pos):
        self.count += self.amount

Also perhaps consider using pos_hint and size_hint as these will help you with consistency with your app running in different devices (size of screen, for instance), rather than using absolute size and/or position.

Hope this helps.

Upvotes: 2

Related Questions