E
E
Egor Letov2019-05-17 15:49:00
Python
Egor Letov, 2019-05-17 15:49:00

Can you help me improve my Python code?

from kivy.app import App
from kivy.uix.button import Button
from kivy.config import Config
from random import random

Config.set('graphics', 'resizable', '0');
Config.set('graphics', 'width', '800');
Config.set('graphics', 'height', '400');

x = 1

class MyApp(App):
    def build(self):
        self.button = Button(
            text = 'Your score: ' + str(x),
            on_press = self.MainEvent,
            font_size = 40,
            background_color = [1, 1, 1, 1])
        return self.button

    def MainEvent(self, instance):
        self.button.text = self.button.text
        self.button.background_color = [
        random(),
        random(),
        random(),
        random()
        ]
        global x
        x += 1

MyApp().run()

I made this source code, the clicker application, but when I run the number of your account, and this is the variable x, it remains equal to 1, and the command x += 1 is not executed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pcdesign, 2019-05-17
@pcdesign

Try adding __init__ to class MyApp(App):

def __init__(self):
    self.x = 1

And then just like this, without any global
self.x += 1

A
abbrakadabbra, 2019-05-17
@abbrakadabbra

Why global in a class? You can declare it inside the __init__ constructor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question