jeudi 28 novembre 2019

How can I generate a new item randomly from a list every time a button is pressed?

I am learning how to use kivy, and I made an example code meant to select a new random item from a list every time a button is pressed. When I run this code, I get the error: 'line 19, in NewChoice number += 1 UnboundLocalError: local variable 'number' referenced before assignment'. How can I reference number in the NewChoice function?

Code:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.screenmanager import Screen
import random

RandomList = ["First", "Second", "Third", "Fourth", "Fifth"]
number = 0

class GameScreen(Screen):

    def __init__(self, **args):
        Screen.__init__(self, **args)
        self.item = random.sample(RandomList, 5)
        self.label = Label(text=str(self.item[number]), color=(1,0,0,1), font_size=(45),size_hint=(0.2,0.1), pos_hint={"center_x":0.5, "center_y":0.9})
        self.add_widget(self.label)

    def NewChoice(self):
        number += 1
        self.label.text = str(self.item[number])

Builder.load_string('''
<GameScreen>:
    name: "GameScreen"
    canvas:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

    Button:
        size: self.texture_size
        on_release: root.NewChoice()
        text: "Press"
        font_size: 50
        color: 1,1,1,1
        background_color: (0,0,0,1)
        background_normal: ""
        background_down: ""
        size_hint: None, None
        pos_hint: {"center_x":0.5, "center_y":0.6}
        width: self.texture_size[0] + dp(10)
        height: self.texture_size[1] + dp(10)
    ''')

class TestApp(App):
    def build(self):
        return GameScreen()

if __name__ == '__main__':
    TestApp().run()



Aucun commentaire:

Enregistrer un commentaire