Answer the question
In order to leave comments, you need to log in
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()
Answer the question
In order to leave comments, you need to log in
Try adding __init__ to class MyApp(App):
def __init__(self):
self.x = 1
self.x += 1
Why global in a class? You can declare it inside the __init__ constructor.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question