Reputation: 31
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
class body(BoxLayout):
def __init__(self, **kwargs):
super(body,self).__init__(**kwargs);
self.orientation = 'vertical';
lst = ['button'];
cont = len(lst);
for d in lst:
eval('self.'+d+'()');
self.add(cont);
def button(self):
self.hLayout_1 = AnchorLayout(orientation='horizontal');
self.button_1 = Button(text='conectar',size_hint=(None,None),size=(100,100));
self.button_2 = Button(text='enviar',size_hint=(None,None),size=(100,100));
self.hLayout_1.add_widget(self.button_1);
self.hLayout_1.add_widget(self.button_2);
def add(self,num):
for n in range(num):
eval('self.add_widget(self.hLayout_'+str(n+1)+')');
class Client(App):
def build(self):
b = body();
return b
Client().run();
buttons leave me one on another, as I fix this?
or also with gridlayout
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
class body(BoxLayout):
def __init__(self, **kwargs):
super(body,self).__init__(**kwargs);
self.pos_hint={'center_x':.5}
self.orientation = 'vertical';
lst = ['button'];
cont = len(lst);
for d in lst:
eval('self.'+d+'()');
self.add(cont);
def button(self):
self.hLayout_1 = GridLayout(cols=2);
self.button_1 = Button(text='conectar',size_hint=(None,None),size=(100,100));
self.button_2 = Button(text='enviar',size_hint=(None,None),size=(100,100));
self.hLayout_1.add_widget(self.button_1);
self.hLayout_1.add_widget(self.button_2);
def add(self,num):
for n in range(num):
eval('self.add_widget(self.hLayout_'+str(n+1)+')');
class Client(App):
def build(self):
b = body();
return b
Client().run();
I can focus as the two buttons correctly?
I've tried with various layout, but it has not worked, maybe it's because it still did not quite understand that sirver "size_hint" appreciate some help.
Upvotes: 0
Views: 3414
Reputation: 14814
Ok, I think this is what you want:
class Body(FloatLayout): # capitalize class per convention, and extend FloatLayout instead of BoxLayout
def __init__(self, **kwargs):
super(Body, self).__init__(**kwargs)
# create a sized BoxLayout
centerlayout = BoxLayout(size_hint=(None, None), size=(200, 100))
# create and add buttons - they will be sized automatically in the BoxLayout
button_1 = Button(text='conectar')
button_2 = Button(text='enviar')
centerlayout.add_widget(button_1)
centerlayout.add_widget(button_2)
# add the BoxLayout
self.add_widget(centerlayout)
# center the BoxLayout to our center - this needs to be bound, as Body is not yet positioned
self.bind(center=centerlayout.setter('center'))
You should also look into the kv language; it makes things much easier, especially as your widgets get more complex.
from kivy.lang import Builder
body = Builder.load_string('''
FloatLayout:
BoxLayout:
size_hint: None, None
size: 200, 100
center: root.center
Button:
text: 'conectar'
Button:
text: 'enviar'
''')
Upvotes: 2
Reputation: 29460
buttons leave me one on another, as I fix this?
This is because you use an AnchorLayout. It aligns its children to the anchor, not to each other, so it's correct behaviour that they are placed in the same place.
The solution is to not use the AnchorLayout at all.
I can focus as the two buttons correctly?
Can you explain what you mean by this?
Also:
eval('self.'+d+'()');
is bad style, you could use getattr
instead, e.g. getattr(self, d)()
.Upvotes: 1