Я хочу создать флажок в моем приложении kivy.
Я хочу создать флажок, который при установке флажка изменит результат функции.
import random
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
from kivy.uix.checkbox import CheckBox
class MyGrid(Widget):
inimigos = ObjectProperty(None)
def Gerar(self):
self.show_inimigos.text = ""
int_inimigos = int(self.inimigos.text)
tipo = ["NEUTRO", "HOSTIL", "ALIADO"]
racas = ["HUMANO", "ELFO", "ANÃO", "HOBBIT", "VIASHINO", "LEONINO", "NEZUMI", "GOBLIN", "NAGA"]
classe = ["FIGHTER", "KNIGHT", "BERSERKER", "FENCER", "NINJA", "HOPLITE", "BEAST TAMER",
"DRAGON TAMER", "ARCHER", "ROUGE", "DOLL MASTER", "WARLOCK", "MAGE", "ACOLYTE", "MONK", "CLERIC",
"BARD", "WARLORD", "SHAMAN", "DRUID", "SPEAKER", "MERCHANT", "ALCHEMIST", "BLACKSMITH", "CHEF"]
elemento = ["FIRE", "AIR", "WATER", "EARTH", "LIGHT", "DARK"]
for i in range(int_inimigos):
escolha_tipo = random.choice(tipo)
escolha_raca = random.choice(racas)
escolha_classe = random.choice(classe)
escolha_elemento = random.choice(elemento)
encontro = (escolha_raca + ' : ' + escolha_classe + ' - ' + escolha_elemento)
self.tipo.text = "ENCONTRO: " + escolha_tipo
if escolha_tipo == "ALIADO":
self.tipo.color = 0.2, 0.7, 0.2, 1
elif escolha_tipo == "NEUTRO":
self.tipo.color = 0.5, 0.5, 0.5, 1
elif escolha_tipo == "HOSTIL":
self.tipo.color = 0.7, 0.2, 0.2, 1
self.show_inimigos.text += encontro + "\n\n"
class My1App(App):
def build(self):
return MyGrid()
if __name__ == "__main__":
My1App().run()
thisмой код kv
<MyGrid>:
inimigos: inimigos
show_inimigos: show_inimigos
tipo: tipo
GridLayout:
cols: 1
size: root.width, root.height
GridLayout:
cols: 2
Label:
text: "Numero de inimigos"
font_size: 20
size_hint: 1,0.2
TextInput:
id: inimigos
multiline: False
size_hint: 1,0.2
GridLayout:
cols: 1
Label:
text: "Racas para o encontro"
GridLayout:
cols: 2
size_hint: 1, 0.2
Label:
text: "Humano"
Checkbox:
Label:
text: "Elfo"
Checkbox:
Label:
text: "Anao"
Checkbox:
Label:
text: "Orc"
Checkbox:
Label:
text: "Hobbit"
Checkbox:
Label:
text: "Nezumi"
Checkbox:
Label:
text: "Leonino"
Checkbox:
Label:
text: "Goblin"
Checkbox:
Label:
text: "Naga"
Button:
text: "Gerar"
size_hint: 1, 0.1
font_size: 20
on_press:
root.Gerar()
Label:
id: tipo
text: "Tipo de encontro"
size_hint: 1,0.2
font_size: 20
Label:
id: show_inimigos
text: "Clique para gerar"
Что я могу сделать, чтобы этот код работал?