python - 删除_Widget 不起作用

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

最佳答案

问题在于您创建了一个新的小网格,而不是编辑现有的小网格。
我还稍微改变了你的程序的结构。 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 - 删除_Widget 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43660950/

相关文章:

Python控制台默认十六进制显示

python - 为什么这两种方法打印出不同的结果?

python - Django、python、mod_wsgi 和 Apache worker

python - Kivy ListView excel 文件

python - 如何在 Kivy 中制作重复的旋转动画?

python - 如何在 Linux 上打包 Kivy IOS 应用程序?

python - 如何在kivy中获取AnchorLayout child 的高度?

Python 正则表达式 : How to implement a regex expression that checks for matching set of brackets?

python - 跨多个列计算字符串值以创建新的总计列的有效方法

python - Ubuntu 中的 Kivy 安装错误