【发布时间】:2020-02-04 17:21:39
【问题描述】:
我正在尝试为我和朋友一起玩的游戏创建一个应用程序。
我有这段代码,我想在 FourWindow 中循环列表“jugadores”。
import kivy
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.core.window import Window
Window.size = (300, 1280)
Jugadores = []
CantidadDeCartas = 0
Objetivo = 0
Manos = 0
class MainWindow(Screen):
cartas = ObjectProperty(None)
objetivo = ObjectProperty(None)
def btn(self):
CantidadDeCartas = int(self.cartas.text)
Objetivo = int(self.objetivo.text)
self.cartas.text = ""
self.objetivo.text = ""
class SecondWindow(Screen):
cartas = ObjectProperty(None)
objetivo = ObjectProperty(None)
class ThirdWindow(Screen):
def btn3(self):
Nombre = self.njugador.text
Jugadores.append(Nombre)
self.njugador.text = ""
self.jugador.text = str(Jugadores)
print(Jugadores)
def btn4(self):
self.njugador.text = ""
Jugadores.clear()
self.jugador.text = ""
def btn5(self):
Jugadores.reverse()
if len(Jugadores) != 0:
largo = Jugadores.count(self)
Jugadores.pop(largo)
Jugadores.reverse()
print(Jugadores)
self.jugador.text = str(Jugadores)
else:
self.jugador.text = ""
class FourWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("tim4.kv")
class tim4App(App):
def build(self):
return kv
if __name__ == "__main__":
tim4App().run()
基维
WindowManager:
MainWindow:
SecondWindow:
ThirdWindow:
FourWindow:
<MainWindow>:
name: "main"
cartas : cartas
objetivo : objetivo
GridLayout:
cols:1
GridLayout:
cols: 2
Label:
text:"Cartas: "
TextInput:
id: cartas
multiline: False
input_type: "number"
input_filter: "int"
Label:
text:"Objetivo: "
TextInput:
id: objetivo
multiline: False
input_type: "number"
input_filter: "int"
Button:
text: "Siguiente>>"
on_release:
app.root.current= "second"
root.manager.screens[1].ids.carta.text = "Se juega con " + root.ids.cartas.text + " cartas"
root.manager.screens[1].ids.objetivo.text = "Se juega a " + root.ids.objetivo.text
root.manager.screens[3].ids.iduno.text = "Se juega con " + root.ids.cartas.text + " cartas"
root.manager.screens[3].ids.iddos.text = "Se juega a " + root.ids.objetivo.text
root.btn()
root.manager.transition.direction="left"
<SecondWindow>:
name: "second"
carta: carta
objetivo: objetivo
GridLayout:
cols:1
size: root.width, root.height
pos: 0, 0
Label:
id: carta
text: ""
Label:
id: objetivo
text: ""
GridLayout:
cols:2
Button:
text: "Ir Atrás"
on_release:
app.root.current= "main"
root.manager.transition.direction="right"
Button:
text: "Elegir Jugadores"
on_release:
app.root.current= "third"
root.manager.transition.direction="left"
<ThirdWindow>
name: "third"
njugador: njugador
jugador: jugador
empezar: empezar
GridLayout:
cols: 2
orientation: "horizontal"
GridLayout:
cols:1
TextInput:
id: njugador
text: "quien juega"
multiline: False
GridLayout:
cols:3
Button:
text: "Agregar Jugador"
on_press: root.btn3()
Button:
text:"Borrar Último"
on_press: root.btn5()
Button:
text:"Limpiar todo"
on_press: root.btn4()
GridLayout:
cols:1
orientation: "vertical"
Label:
id : jugador
text: ""
Button:
id: empezar
text:"Empezar Juego"
on_release:
app.root.current= "four"
root.manager.transition.direction="left"
<FourWindow>
name: "four"
iduno: iduno
iddos: iddos
idtres: idtres
GridLayout:
orientation:"horizontal"
rows: 3
Label:
id: iduno
text:""
Label:
id: iddos
text:""
Label:
id: idtres
text:"manos"
在 FourWindow 上,我希望循环列表“Jugadores”以创建标签。反正有通过kv语言做到这一点吗?或者如何在python文件上做到这一点?
【问题讨论】:
-
您可以向您的
FourWindow类添加一个on_enter()方法来创建标签。
标签: python-3.x loops kivy kivy-language