我尝试将 2 个盒子布局分开,但它们融合在一起

I try to separate 2 box layouts but instead they fuse together

我想将这 2 个框布局分开,但是当我 运行 时,它们实际上是一个重叠的。我已经查看了 kivy 文档,但仍然没有找到答案。

这是我的代码:

<BoxLayout>:
    spacing:"0dp"
    orientation:"vertical"
    Label:
        text:"test"
        background_color: (64/255, 64/255, 64/255,1)
        size_hint:1,1
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                size: self.size
                pos: self.pos
    Label:
        text:"test"
        background_color: (64/255, 64/255, 64/255,1)
        size_hint:.2,.3
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                size: self.size
                pos: self.pos
    Label:
        text:"test"
        background_color: (64/255, 64/255, 64/255,1)
        size_hint:.2,.3
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                size: self.size
                pos: self.pos

<BoxLayout>:
    orientation:"vertical"
    spacing:"10dp"
    TextInput:
        text: "type enemy name"
        size_hint:1,3
        id: starta
    Button:
        text:"Start"
        on_press: root.on_button_click()
        size_hint:1,3
        font_size:40
        id: startb
    Label:
        id: label
        text: "Enter enemy name and press 'start'"
        text_size: self.width-100, None
        font_size:27
    Button:
        text:"Fight"
        size_hint: 1,.3
        color:1,0,0,1
        background_color:.5,0,0
        font_size:40
        opacity:0
        id: thingy
    Button:
        text:"Guard"
        size_hint: 1,.3
        color:0,0,1,1
        background_color:0,0,.5
        font_size:40
        opacity:0
        id: thingys
    Button:
        text:"Heal"
        size_hint: 1,.3
        color:0,1,0,1
        background_color:0,.5,0
        font_size:40
        opacity:0
        id: thingyss
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import (NumericProperty, StringProperty)
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from random import randint
from time import sleep
from threading import Thread
import time
from kivy.core.audio import SoundLoader
turn=False
started=False
attacking=False
healing=False
guard=False
guardcor=False
enemycharge=False
enemyhp=200
playerhp=200
maxmana=50
mana=50
enemydamage=20
playerdamage=13
testify=[]

Builder.load_string("""
<BoxLayout>:
    spacing:"0dp"
    orientation:"vertical"
    Label:
        text:"test"
        background_color: (64/255, 64/255, 64/255,1)
        size_hint:1,1
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                size: self.size
                pos: self.pos
    Label:
        text:"test"
        background_color: (64/255, 64/255, 64/255,1)
        size_hint:.2,.3
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                size: self.size
                pos: self.pos
    Label:
        text:"test"
        background_color: (64/255, 64/255, 64/255,1)
        size_hint:.2,.3
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                size: self.size
                pos: self.pos

<BoxLayout>:
    orientation:"vertical"
    spacing:"10dp"
    TextInput:
        text: "type enemy name"
        size_hint:1,3
        id: starta
    Button:
        text:"Start"
        on_press: root.on_button_click()
        size_hint:1,3
        font_size:40
        id: startb
    Label:
        id: label
        text: "Enter enemy name and press 'start'"
        text_size: self.width-100, None
        font_size:27
    Button:
        text:"Fight"
        size_hint: 1,.3
        color:1,0,0,1
        background_color:.5,0,0
        font_size:40
        opacity:0
        id: thingy
    Button:
        text:"Guard"
        size_hint: 1,.3
        color:0,0,1,1
        background_color:0,0,.5
        font_size:40
        opacity:0
        id: thingys
    Button:
        text:"Heal"
        size_hint: 1,.3
        color:0,1,0,1
        background_color:0,.5,0
        font_size:40
        opacity:0
        id: thingyss
        
""")

from time import sleep
from threading import Thread


class ProcessingThread(Thread):
    global testify
    global enemyhp
    global playerhp
    global maxmana
    global mana
    global enemydamage
    global playerdamage
    global attacking
    global healing
    global guard
    global turn
    global enemycharge
    global guardcor
    def run(self):
        global attacking
        global healing
        global guard
        global turn
        global testify
        global enemyhp
        global playerhp
        global maxmana
        global mana
        global enemydamage
        global playerdamage
        global enemycharge
        global guardcor
        label = testify[0]
        fightb = testify[1]
        guardb = testify[2]
        healb = testify[3]
        enemyname=testify[4]
        rootui=testify[5]
        #enemyhpui=testify[6]
        #playerhpui=testify[7]
        #manaui=testify[8]
        fightb.opacity = 1
        guardb.opacity = 1
        healb.opacity = 1

        TextBox.type_text(rootui, "who you are fighting againist is %s, be careful. they are strong."% enemyname)
        sleep(7)
        label.color=1,0,0,1
        TextBox.type_text(rootui, "FIGHT makes you deal 13 damage to the enemy.")
        sleep(5)
        label.color=0,0,1,1
        TextBox.type_text(rootui,"GUARD makes you not get hit when the enemy attacks.")
        sleep(5.5)
        label.color=0,1,0,1
        TextBox.type_text(rootui, "and lastly, HEAL makes you heal 30 health.")
        sleep(5)
        label.color=1,1,1,1
        TextBox.type_text(rootui, "good luck, player.")
        sleep(3)
        soundyeye = SoundLoader.load('battlesong.mp3')
        soundyeye.loop=True
        soundyeye.play()
        TextBox.type_text(rootui, "%s jumps into the battlefield!"% enemyname)
        while True:
            dialoguerng=randint(1,4)
            if dialoguerng==1:
                TextBox.type_text(rootui,"%s looks around him and then looks back to you."% enemyname)
                sleep(6)
            elif dialoguerng==2:
                TextBox.type_text(rootui,"%s readies itself."% enemyname)
                sleep(3)
            elif dialoguerng==3:
                TextBox.type_text(rootui,"%s is too excited to beat you up"% enemyname)
                sleep(5)
            elif dialoguerng==4:
                TextBox.type_text(rootui, "%s is sick of this battling system"% enemyname)
                sleep(5)
            label.text="what are you going to do?"
            turn=True
            while True:
                sleep(0.1)
                if attacking==True:
                    turn=False
                    attacking==False
                    TextBox.type_text()
                    break
                elif guard==True:
                    turn=False
                    guard=False
                    guardcor==True
                    break
                elif healing==True:
                    turn=False
                    healing==False

                    break



class TextBox(BoxLayout):
    cache_text = StringProperty("") # For storing the entered text.
    index = NumericProperty(0) # For iteration over cache_text.
    global started
    global testify
    def hide_widget(self, wid, dohide=True):
        if hasattr(wid, 'saved_attrs'):
            if not dohide:
                wid.height, wid.size_hint_y, wid.opacity, wid.disabled = wid.saved_attrs
                del wid.saved_attrs
        elif dohide:
            wid.saved_attrs = wid.height, wid.size_hint_y, wid.opacity, wid.disabled
            wid.height, wid.size_hint_y, wid.opacity, wid.disabled = 0, None, 0, True

    def type_text(self, txt, time=0.06):
        global testify
        self.ids.label.text = ""  # Comment it out to retain the text.
        self.cache_text = txt # Store the entered text here.
        # For more control you may use method Clock.create_trigger.
        self.ev = Clock.schedule_interval(self.update_text, time) # Update text after every 0.25 sec.

    def update_text(self, *args):
            global testify
            if self.index < len(self.cache_text):
                val = self.cache_text[self.index]
                self.ids.label.text += val # Appending to existing text.
                self.index += 1
            else:
                self.index = 0 # Reset index.
                self.cache_text = "" # Clear cache.
                self.ev.cancel() # Cancel text updation.

    def on_button_click(self):
        global testify
        global started
        if started==False:
            started=True
            enemyname = self.ids.starta.text
            self.hide_widget(self.ids.starta)
            self.hide_widget(self.ids.startb)
            testify.insert(0,self.ids.label)
            testify.insert(1,self.ids.thingy)
            testify.insert(2,self.ids.thingys)
            testify.insert(3,self.ids.thingyss)
            testify.insert(4,enemyname)
            testify.insert(5,self)
            p = ProcessingThread()
            p.start()

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

TestApp().run()

如果脚本很糟糕,也不要怪我我是新手,我是 5 天前开始的(如果你问为什么“第一个”boxlayout 的 3 个标签中的一个与另外 2 个不同,这是为了测试目的) gui layout output gui layout output 2

当您使用 <SomeClass>kv 文件中定义规则时,该规则将应用于在 kv 文件之后创建的 SomeClass 的每个实例已加载。如果您有两个不同的规则,例如 <SomeClass>,那么第二个规则的第二组属性只会添加到第一个规则中。

如果你想要不同的属性,你可以创建不同的类并定义不同的<>规则。或者,您可以定义一个规则,其中包含原始 SomeClass.

的两个实例

在你的情况下,你可以这样做:

FloatLayout:
    TextBox: 
        # properties of the first TextBox
        .
        .
        .
    TextBox: 
        # properties of the second TextBox
        .
        .
        .

当然,您需要对 TextBox 个实例进行定位和调整大小。

您可以使用 <TextBox> 规则来定义对 TextBox.

的所有实例通用的属性