【问题标题】:like putting two buttons centered in kivy?就像把两个按钮放在kivy的中心?
【发布时间】:2014-06-15 19:30:44
【问题描述】:
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();

我可以正确聚焦两个按钮吗?

我尝试了各种布局,但都没有成功,可能是因为还不太明白,sirver "size_hint" 感谢一些帮助。

【问题讨论】:

  • 1) 你问的是如何让两个按钮并排显示?不太确定你想要什么。 2) 你不应该在 Python 中使用分号 (;) - 它们只用于将多个语句放在一行上,无论如何这是不好的形式。 3) eval() 速度慢且不安全。使用getattr()通过字符串获取属性。
  • 如果我想要一个紧挨着另一个的按钮,并且两个在中间,专注于一个布局,假装不生气,我会尽量不放;最后,还要感谢“eval()”,我将使用 getattr()。那么如何才能并排出现在中心呢?什么是正确的布局?

标签: python button center kivy


【解决方案1】:

好的,我想这就是你想要的:

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

【讨论】:

  • 非常感谢您的回答,对我帮助很大
  • 它对你有用吗?如果是,你应该接受它作为这个问题的答案。我正在测试它:)
【解决方案2】:

按钮让我一个接一个,当我解决这个问题时?

这是因为您使用了 AnchorLayout。它将子节点与锚点对齐,而不是彼此对齐,因此将它们放置在同一个位置是正确的行为。

解决方案是根本不使用 AnchorLayout。

我可以正确聚焦两个按钮吗?

你能解释一下你的意思吗?

还有:

  • 使用 python,行尾不需要分号。
  • eval('self.'+d+'()'); 风格不好,您可以改用 getattr,例如getattr(self, d)()

【讨论】:

  • 我知道这不是必需的,但我喜欢将代码编写为 那么如何聚焦按钮?什么是正确的方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-04
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
相关资源
最近更新 更多