Forum Archive

[SOLVED] What's wrong with my code?

Pythonistapro777

The click variable is meant to go up by one everytime I click the button. What do I do?. :(

Thanks in advance!

Here's the code:


from scene import *
from PIL import Image
clicks = 0
class JitterClick(Scene):
    def setup(self):
        self.button = Button(Rect(self.size.w/2-100, self.size.h/2-140, 200, 200))
        self.button.background = Color(0,0,0)
        self.button.stroke = Color(0,0,0)
        self.button.image = 'Red_Circle'
        self.button.action = self.add_clicks
    def add_clicks(sender):
        global clicks
        clicks += 1
    def draw(self):
        self.button.draw()
        text('Clicks: %i' % clicks, x=self.size.w/2, y=self.size.h/4*3, font_size=64)
run(JitterClick())
omz

@Pythonistapro777 Please don't open multiple topics for the same thing. You can edit your existing posts (or reply to them) if you need to add more details or clarify something.

Pythonistapro777

Sorry

Pythonistapro777

Lmao, I still need help

omz

Here's a very simple scene that counts clicks/touches, hope that helps:

from scene import *
clicks = 0

class ClickScene(Scene):
    def touch_began(self, touch):
        # the "global" keyword is important here:
        global clicks
        clicks += 1

    def draw(self):
        background(0, 0, 0)
        text('Clicks: %i' % clicks, 'Helvetica', 30, self.size.w/2, self.size.h/2, 5)

run(ClickScene())
Pythonistapro777

I NEED HELP AGAIN!!!

Pythonistapro777

@omz what can I do about the global error

omz

What "global error"? You need to be a little more specific.

Pythonistapro777

When the code is run there is a global error on the text().

Phuket2

Why not in your setup method, just do self.clicks = 0. Then reference self.clicks thereafter. I had to do that before in your previous code to get it to run. No need for the global.

Phuket2

This should work

from scene import *
#clicks = 0

class ClickScene(Scene):
    def setup(self):
        self.clicks = 0

    def touch_began(self, touch):
        # the "global" keyword is important here:
        #global clicks
        #clicks += 1
        self.clicks += 1

    def draw(self):
        background(0, 0, 0)
        # clicks, changed to self.clicks below
        text('Clicks: %i' % self.clicks, 'Helvetica', 30, self.size.w/2, self.size.h/2, 5)

run(ClickScene())
filippocld223

Man, stop writing in caps and pretending answers like a little kid -.-

Pythonistapro777

😑

Pythonistapro777

So much for your help @filippocld

ccc

The global error in the original script is caused by the fact that the first click is singular (without an 's') and the second clicks is plural (with an 's') so they are not the same variable.

Pythonistapro777

Changed the question.

ccc

Add the following line as the last line of setup():

        self.add_layer(self.button)

This will make the counter update but you will have a screen refresh problem to solve. See the draw() methods that @omz and @Phukett2 have provided above for a partial answer to your refresh problem.

Pythonistapro777

THANK YOU!

Pythonistapro777

I figured out how to fix the screen refresh error.
Thanks a lot everyone!