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();
按钮让我一个接一个,我解决了这个问题?
或者也可以使用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();
我可以正确对焦两个按钮吗?
我尝试了各种布局,但它没有奏效,也许是因为它仍然不太明白sirver“size_hint”非常感谢一些帮助。
答案 0 :(得分:2)
好的,我认为这就是你想要的:
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'))
您还应该查看kv language;它使事情变得更容易,特别是当你的小部件变得更复杂时。
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'
''')
答案 1 :(得分:1)
按钮让我一个接一个,我解决了这个问题?
这是因为你使用了AnchorLayout。它将其子项与锚点对齐,而不是彼此对齐,因此将它们放置在同一位置是正确的行为。
解决方案是根本不使用AnchorLayout。
我可以正确对焦两个按钮吗?
你能解释一下你的意思吗?
此外:
eval('self.'+d+'()');
风格不好,您可以改用getattr
,例如getattr(self, d)()
。