首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我试着把两个盒子的布局分开,但它们却融合在一起。

我试着把两个盒子的布局分开,但它们却融合在一起。
EN

Stack Overflow用户
提问于 2022-02-13 18:57:23
回答 1查看 36关注 0票数 0

我想分开这两个盒子布局,但当我运行它,他们是字面上的一个比另一个。我已经翻阅了几份文件,但还是没有找到答案。

这是我的代码:

代码语言:javascript
复制
<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
代码语言:javascript
复制
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天前开始的那样(如果你问为什么“第一个”框布局中的3个标签中的一个与另外两个不同,这是为了测试目的) gui布局输出 gui布局输出2

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-14 04:03:47

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

如果需要不同的属性,可以创建不同的类并定义不同的<>规则。或者,可以定义包含原始SomeClass的两个实例的单个规则。

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

代码语言:javascript
复制
FloatLayout:
    TextBox: 
        # properties of the first TextBox
        .
        .
        .
    TextBox: 
        # properties of the second TextBox
        .
        .
        .

当然,您需要定位和调整TextBox实例的大小。

您可以使用<TextBox>规则来定义所有TextBox实例所共有的属性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71103903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档