Reputation: 15
I am trying to change the canvas size of an image randomly. I have 10 images in a GridLayout and I want to randomly resize one of them. I have tried several ways but I can't figure it out, what am I doing wrong?
.kv
<ButOpcion>:
canvas.before:
Rectangle:
pos: self.pos
size: 250, 250
source: "data/img/letra_d/dragon.png"
<ContainerCaza>:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'data/img/fondobosque.jpg'
but_1: but1
but_2: but2
but_3: but3
but_4: but4
but_5: but5
but_6: but6
but_7: but7
but_8: but8
but_9: but9
but_10: but10
BoxLayout:
orientation: 'vertical'
Label:
text: 'Ahora debes localizar al intruso entre estas imágenes. ¡Fíjate bien, una de las imágenes es distinta!'
size_hint_y: None
text_size: self.width, None
#height: self.texture_size[1]
halign: 'center'
valign: 'middle'
padding: (20,15)
font_size: 25
color: (.66, .38, .14, 1)
GridLayout:
pos_hint: {"center_x": 0.5, "center_y": 0.5}
cols: 5
spacing: '10dp'
#size_hint: 0.6, 0.6
ButOpcion:
id: but1
ButOpcion:
id: but2
ButOpcion:
id: but3
ButOpcion:
id: but4
ButOpcion:
id: but5
ButOpcion:
id: but6
ButOpcion:
id: but7
ButOpcion:
id: but8
ButOpcion:
id: but9
ButOpcion:
id: but10
Button:
size_hint:.06, 0.1
text: "Volver al menú"
on_release: app.root.current = 'menu'
.py
__all__ = ('Caza')
import kivy
kivy.require('1.0.6')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from random import choice, random
from kivy.properties import ObjectProperty
from kivy.graphics import Rectangle
#Builder.load_file('caza.kv')
class ButOpcion(Widget):
pass
class ContainerCaza(Screen):
def __init__(self, **kwargs):
super(ContainerCaza, self).__init__(**kwargs)
def intruso(self):
but_1 = ObjectProperty(None)
but_2 = ObjectProperty(None)
but_3 = ObjectProperty(None)
but_4 = ObjectProperty(None)
but_5 = ObjectProperty(None)
but_6 = ObjectProperty(None)
but_7 = ObjectProperty(None)
but_8 = ObjectProperty(None)
but_9 = ObjectProperty(None)
but_10 = ObjectProperty(None)
target = choice(['but1', 'but2', 'but3', 'but4', 'but5', 'but6', 'but7', 'but8', 'but9', 'but10'])
print(target)
if target == 'but1':
def ChangeImage1(self):
self.ids.but1.size = 10, 10
elif target == 'but2':
def ChangeImage2(self):
self.ids.but2.size = 10, 10
elif target == 'but3':
def ChangeImage3(self):
self.ids.but3.size = 10, 10
elif target == 'but4':
def ChangeImage4(self):
self.ids.but4.size = 10, 10
elif target == 'but5':
def ChangeImage5(self):
self.ids.but5.size = 10, 10
elif target == 'but6':
def ChangeImage6(self):
self.ids.but6.size = 10, 10
elif target == 'but7':
def ChangeImage7(self):
self.ids.but7.size = 10, 10
elif target == 'but8':
def ChangeImage8(self):
self.ids.but8.size = 10, 10
elif target == 'but9':
def ChangeImage9(self):
self.ids.but9.size = 10, 10
else:
def ChangeImage10(self):
self.ids.but10.size = 10, 10
class CazaApp(App):
def build(self):
#Window.fullscreen = 'auto'
caza = ContainerCaza()
caza.intruso()
return caza
if __name__ == "__main__":
CazaApp().run()
How can I access the canvas.before from the .py file? I'm new to python and kivy and I'm a bit lost, there are a lot of things I don't know how to do.
Upvotes: 0
Views: 85
Reputation: 15
Thanks John, it seems to work correctly now, but aesthetically, it places the GridLayout in the upper left corner.
Upvotes: 0
Reputation: 38857
In your intruso()
method, you are defining inner methods that can change the size of a ButOpcion
, but you never call those inner methods. I don't see any purpose to creating an inner method here, so I think you should just execute the code that you are putting in the inner method:
def intruso(self, *args):
target = choice(['but1', 'but2', 'but3', 'but4', 'but5', 'but6', 'but7', 'but8', 'but9', 'but10'])
print(target)
if target == 'but1':
self.ids.but1.size = 10, 10
elif target == 'but2':
self.ids.but2.size = 10, 10
.
.
.
And rather than trying to change the size of the canvas directly, just change the size of the ButOpcion
and let the canvas adjust to it. This can be done by defining your ButOpcion
in the kv
as:
<ButOpcion>:
size_hint: None, None
size: 250, 250
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: "data/img/letra_d/dragon.png"
Upvotes: 1