Reputation: 460
I have highlighted a line below where remove_widget is not functioning correctly. I believe that I have accessed the Class correctly. However, I am unable to add or remove a widget with the code below.
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()
Upvotes: 1
Views: 155
Reputation: 5415
The problem is that you create a new smallgrid, instead of editing the existing one.
Also I changed the structure of your program a bit. The buttonPress
should be a method of the SmallGrid
class instead.
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()
Upvotes: 1