Remove_Widget无法正常工作

时间:2017-04-27 14:41:14

标签: python kivy

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

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()

1 个答案:

答案 0 :(得分:1)

问题在于您创建了一个新的小网格,而不是编辑现有的小网格 我也改变了程序的结构。 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()