【问题标题】:How to loop in Kivy using kv language?如何使用 kv 语言在 Kivy 中循环?
【发布时间】: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


【解决方案1】:

您可以在FourWindow 类中使用on_enter() 方法来实现它,或者您可以通过在kv 中定义on_enter 方法在kv 中实现它。为此,请修改 kv 中的 FourWindow 规则:

#: import Label kivy.uix.label.Label
<FourWindow>
    name: "four"
    iduno: iduno
    iddos: iddos
    idtres: idtres

    on_pre_enter: for p in self.players: l=Label(text=p); grid.add_widget(l);

    GridLayout:
        id: grid   # needed for the above code
        rows: 3

        Label:
            id: iduno
            text:""
        Label:
            id: iddos
            text:""
        Label:
            id: idtres
            text:"manos"

并修改您对FourWindow 类的定义:

class FourWindow(Screen):
    players = ObjectProperty(Jugadores)

players 对象只是提供对Jugadores 列表的访问权限。

【讨论】:

  • 太好了,效果很好。你知道我在哪里可以找到有关 Kivy 的文档。我觉得官方文档没有多大用处。
  • 不确定您要问什么,但documentation 解释了如何在kv 文件中使用python。
猜你喜欢
  • 2015-08-29
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多