I want to display the updated score value but only the initial value is displayed. The score variable in the MainClass() is linked to the kivy file, but the score is not updating. It is always showing 0 whereas it should add 1 whenever the player wins the game. Any solution will be very helpful. Thank You.
main.py
...some code...
class MainClass(Widget):
score = NumericProperty(1)
score = 0
pattern_maker(score) # This is the function to make the pattern.
once_pattern_shown = False
won_lost = ""
checked = False
def update(self, dt):
global button_clicked, pattern_in_count, pattern, check_ans_clicked
start_obj = Start()
if start_pattern_show and not self.once_pattern_shown:
for i in range(0, self.score+1):
if pattern[i] == 'r':
beep1.play()
self.once_pattern_shown = True
elif pattern[i] == 'g':
beep2.play()
self.once_pattern_shown = True
elif pattern[i] == 'b':
beep3.play()
self.once_pattern_shown = True
elif pattern[i] == 'y':
beep4.play()
self.once_pattern_shown = True
if start_pattern_in and check_ans_clicked and pattern_in_count <= self.score+1:
if button_clicked == pattern:
self.won_lost = "won"
else:
self.won_lost = "lost"
elif start_pattern_in and check_ans_clicked and pattern_in_count > self.score+1:
print "No more button clicks are allowed."
elif start_pattern_in and check_ans_clicked:
print "Misfunction."
if button_clicked == pattern and not self.checked:
self.score += 1 # I want this score to be displayed.
self.checked = True
class Test2App(App):
def build(self):
game = MainClass()
Clock.schedule_interval(game.update, 2) # call 'update' func every 1.0s.
return game
if __name__ == '__main__':
Test2App().run()
test2.kv
...some code...
<MainClass>:
canvas:
Color:
rgb: 0, 55, 55
Rectangle:
pos: self.pos
size: root.width, root.height
Label:
pos: 10, root.height-70
font_size: 25
text: "SCORE: " + str(root.score)
...some more code...
答案 0 :(得分:0)
You can update your label from Python code inside your update
method.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.core.window import Window
Builder.load_string('''
<MainClass>:
Label:
id: label
pos: 10, root.height-70
font_size: 25
text: "SCORE: 0"
''')
class MainClass(Widget):
score = NumericProperty(1)
score = 0
def update(self, *args):
self.score += 1
self.ids['label'].text = "SCORE: " + str(self.score)
class Test2App(App):
def build(self):
game = MainClass()
Clock.schedule_interval(game.update, 1) # call 'update' func every 1.0s.
return game
if __name__ == '__main__':
Test2App().run()
答案 1 :(得分:0)
The error is in the line score = 0
, by doing thisscore
stops being an instance of NumericProperty
class to become an int
instance, and therefore it stops generating events when it is modified.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder
from kivy.properties import NumericProperty
Builder.load_string('''
<MainWindow>:
orientation: "vertical"
Label:
id: lb_score
text: "Score " + str(root.score)
Button:
size_hint_y: 0.1
text: "+1"
on_press: root.add_score()
''')
class MainWindow(BoxLayout):
score = NumericProperty(0)
def add_score(self):
self.score += 1
class TestApp(App):
def build(self):
return MainWindow()
if __name__ == "__main__":
TestApp().run()