我在下面的一行中突出显示了一行,其中remove_widget无法正常运行。我相信我已经正确访问了班级。但是,我无法使用以下代码添加或删除小部件。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
import random
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, AliasProperty

class LargeGrid(GridLayout):
    cols = 8
    rows = 8

def __init__(self,**kwargs):
    super(LargeGrid,self).__init__(**kwargs)
    for i in range(64):
        self.add_widget(Button(text=str(i), on_press=buttonPress))

class SmallGrid(BoxLayout):
    def __init__(self,**kwargs):
        super(SmallGrid,self).__init__(**kwargs)
        for i in range(8):
             self.add_widget(Button(text=str(i), background_color= (random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1), 1.0)))

def buttonPress(obj):
    smallGrid = SmallGrid()
    sel = smallGrid.children[-1]

    #These lines work
    obj.background_color = sel.background_color
    obj.text = sel.text

    #This line is not working properly
    smallGrid.remove_widget(sel)

    root = Builder.load_string('''

BoxLayout:
    orientation: 'horizontal'

BoxLayout:
    orientation: 'vertical'

    Button:
        text: "Logo"
        size_hint: (1, .1)
    SmallGrid:
        orientation: 'vertical'

BoxLayout:
    orientation: 'vertical'

    Button:
        text: "Settings"
        size_hint: (1, .1)

    LargeGrid:

 ''')


 class MyApp(App):

     def build(self):
          return root

 MyApp().run()

最佳答案

问题是您创建了一个新的小网格,而不是编辑现有的小网格。
我也稍微改变了程序的结构。 buttonPress应该是SmallGrid类的方法。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
import random


class MyButton(Button):
    pass


class LargeGrid(GridLayout):
    cols = 8
    rows = 8

    def __init__(self,**kwargs):
        super(LargeGrid,self).__init__(**kwargs)
        for i in range(64):
            self.add_widget(MyButton(text=str(i)))


class SmallGrid(BoxLayout):
    def __init__(self,**kwargs):
        super(SmallGrid,self).__init__(**kwargs)
        for i in range(8):
             self.add_widget(Button(text=str(i), background_color= (random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1), 1.0)))


    def buttonPress(self,obj):
        sel = self.children[-1]

        obj.background_color = sel.background_color
        obj.text = sel.text

        self.remove_widget(sel)


root = Builder.load_string('''

<MyButton>:
    on_release:
        app.root.ids.smallgrid.buttonPress(self)


BoxLayout:
    orientation: 'horizontal'

    BoxLayout:
        orientation: 'vertical'

        Button:
            text: "Logo"
            size_hint: (1, .1)
        SmallGrid:
            id: smallgrid
            orientation: 'vertical'

    BoxLayout:
        orientation: 'vertical'

        Button:
            text: "Settings"
            size_hint: (1, .1)

        LargeGrid:

 ''')


class MyApp(App):

    def build(self):
        return root

MyApp().run()

关于python - Remove_Widget不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43660950/

10-09 17:11