如何在Kivy中将两个按钮居中放置?

0 投票
2 回答
3338 浏览
提问于 2025-04-18 04:47
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();

按钮重叠在一起,我该怎么解决这个问题?

或者也可以用网格布局来处理。

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();

我能让这两个按钮正确对齐吗?

我试过不同的布局,但都没有成功,可能是因为我还不太理解“size_hint”这个概念,希望能得到一些帮助。

2 个回答

1

按钮重叠在一起,我该怎么解决这个问题?

这是因为你使用了AnchorLayout(锚点布局)。这种布局是把子元素对齐到锚点,而不是彼此对齐,所以它们重叠在一起是正常的。

解决办法是干脆不使用AnchorLayout。

我能让两个按钮正确地获得焦点吗?

你能解释一下你这句话的意思吗?

另外:

  • 在Python中,行末不需要分号。
  • eval('self.'+d+'()'); 这种写法不太好,建议用 getattr,比如 getattr(self, d)()
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语言;它会让事情变得简单很多,特别是当你的控件变得更复杂的时候。

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'
''')

demo image

撰写回答