首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除BoxLayout?

如何删除BoxLayout?
EN

Stack Overflow用户
提问于 2021-10-16 11:42:40
回答 2查看 43关注 0票数 0

我想在我的小部件中删除Boxlayout,但我不能。

在应用程序的开头有一个名为"Speca“的方框布局。在我选择之后,它必须被删除。

当你运行应用程序时,在“主页”微调器中选择一些东西,然后单击任何新的切换按钮。

任何切换按钮的按下,"Speca“必须消失。

请帮帮忙。

下面是main.py:

代码语言:javascript
复制
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.spinner import Spinner
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, StringProperty, BooleanProperty
from kivy.properties import ListProperty
from collections import OrderedDict
from kivy.uix.togglebutton import ToggleButton

data1=["mother","father","son"]
data2=["uncle","aunt","grandfather"]
data3=["jack","mike","simon"]
data4=["1898","1975","1985","1885"]

amd="0dp"

class MainWidget(Widget):
    def remove_layout(self, *ignore):
        self.remove_widget(self.layout)

    global amd
    an0=tuple(list(OrderedDict.fromkeys(data1)))
    cal5= ObjectProperty()
    cal6= ObjectProperty()
    def btn10(self,text):

        if self.cal5:
            self.cal5.parent.remove_widget(self.cal5)

        self.cal5 =ModelSpecifications()


        a=data2
        b=data3
        c=data4

        mi=[]
        n=0

        while n < len(a):
            aba=n
            mi.append(aba)
            n+=1

        for i in mi:
            self.b1=MyTButton(text=str(i),size_hint=(1,None),height="100dp",group="selections")
            self.cal5.add_widget(self.b1)
        self.ids.scd.add_widget(self.cal5, index=3) 

    def on_state(self, togglebutton): #<----THIS IS THE TUGGLEBUTTON I WANT USE
        global amd
        tb = togglebutton
        text1=tb.text

        if text1=="0":
            amd="50dp"
        elif text1=="1":
            amd="100dp"
        else:
            amd="200dp"

        if self.cal6:
            self.cal6.parent.remove_widget(self.cal6)

        self.cal6 =Spec()
        self.ids.scd.add_widget(self.cal6, index=2)

class SecondPage(ScrollView):
    pass


class Speca(BoxLayout):  #<------ THIS IS THE BOXLAYOUT I WANT TO REMOVE
    pass


class Spec(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.size_hint=(1,None)
        self.height=amd


class MyTButton(ToggleButton):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        

class ModelSpecifications(BoxLayout): #this is the class I want add after my spinner selection 
    pass

class Calculation(GridLayout):
    pass   

class MyApp(App):
    pass

MyApp().run()

下面是my.kv:

代码语言:javascript
复制
MainWidget:
<MainWidget>:
    hideable: hideable
    ScreenManager:
        id: scmanager
        size: root.width, root.height
        Screen:
            id: scndpage
            name: "second"
            SecondPage:
                Calculation:
                    id:scd            
                    cols:1 
                    height: self.minimum_height 
                    row_default_height: "70dp"
                    size_hint_y: None
                    spacing:"10dp"
                    canvas.before:
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    BoxLayout:

                        size_hint: 1, None
                        height: "50dp"
                        
                        pading:"10dp"
                        spacing:"10dp"
                        orientation: "vertical"
                        BoxLayout:
                            orientation: "horizontal"
                            Label:
                                text:"Name:"
                                color: 0,0,0,1
                            TextInput:
                                text:"---"
                                color: 0,0,0,1
                            Label:
                                text:"Surname:"
                                color: 0,0,0,1
                            TextInput:
                                text:"-----"
                                color: 0,0,0,1
                    BoxLayout:
                        id:scdd
                        size_hint: 1, 1
                        height: "100dp"
                        orientation: "vertical"
                        BoxLayout:
                            size_hint: 1, None
                            height: "50dp"
                            orientation: "horizontal"
                            Label:
                                text: " Sellection:"
                                color: 0,0,0,1
                            Spinner:
                                text: 'Home'
                                values: root.an0
                                on_text: app.root.btn10(self.text)   

                    Speca: #<------ THIS IS THE BOXLAYOUT I WANT TO REMOVE

                    Button:
                        text:" Calculate"

                    Button:
                        text:"Sellect"
                    
                    Button:
                        text:"Back"

    
<ModelSpecifications>:      
    id:anss                 
    pading:"10dp"
    spacing:"10dp"
    size_hint: 1, None
    height: "100dp"
    orientation: "horizontal"


<MyTButton@ToggleButton>: #<----THIS IS THE TUGGLEBUTTON I WANT USE
    on_state: 
        app.root.on_state(self)

<Speca>:   #<------ THIS IS THE BOXLAYOUT I WANT TO REMOVE
    orientation:"vertical"
    Label:  
        color: (1,1,0,1)
        text:"I WANT TO REMOVE THIS PART WHEN I PRESS ANY OF TOGGLE BUTTONS"

请运行应用程序并查看它。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-16 12:27:08

在我看来,speca是ID为scd的计算小部件的子级。因此,给speca一个ID,然后在on_srate python函数中通过ID删除speca。

Kv

代码语言:javascript
复制
SecondPage:
    Calculation:
        id:scd
        speca:
            id: speca

py

代码语言:javascript
复制
def on_state():
    self.root.ids.scd.remove_widget(self.root.ids.speca)
票数 0
EN

Stack Overflow用户

发布于 2021-10-16 19:13:41

是。

当我做出这些改变的时候;

Kv

代码语言:javascript
复制
SecondPage:
    Calculation:
        id:scd
        speca:
            id: speca

py

代码语言:javascript
复制
def on_state():
    self.ids.scd.remove_widget(self.ids.speca)

它起作用了。

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

https://stackoverflow.com/questions/69595271

复制
相关文章

相似问题

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