python - 用 Kivy 居中和包裹

标签 python python-3.x kivy

我正在 Kivy 中构建一个 GUI,其中窗口需要水平调整大小。我有三个按钮,两个小按钮位于一个大按钮的两侧,我想在窗口变得太窄时更改位置。

示例图片

按钮需要居中对齐。我可以通过在任一侧使用填充来做到这一点,但是当窗口变得太小时,填充也会移动到新行,从而阻止按钮居中对齐。我还尝试过使用堆栈布局进行实验,但没有成功地使任何内容都能正确对齐。

有没有直接解决这个问题的方法?

最小工作示例:

from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window


class Home(Widget):
    def __init__(self, **kwargs):
        super(Home, self).__init__(**kwargs)

        BUTTON_WIDTH = 0.1
        BUTTON_HEIGHT = 0.1

        layout = StackLayout(orientation='lr-tb',
                             size=(Window.width, Window.height),
                             pos=self.pos
                             )

        self.add_widget(layout)

        button1 = Button(text='1',
                         size_hint=(BUTTON_WIDTH, BUTTON_HEIGHT)
                         )
        big_button = Button(text='big button',
                            size_hint=(3*BUTTON_WIDTH, BUTTON_HEIGHT)
                            )
        button2 = Button(text='2',
                         size_hint=(BUTTON_WIDTH, BUTTON_HEIGHT)
                         )

        layout.add_widget(button1)
        layout.add_widget(big_button)
        layout.add_widget(button2)


class TestApp(App):
    def build(self):
        return Home()


if __name__ == '__main__':
    TestApp().run()

最佳答案

对于在您的应用中进行重大更改,我深表歉意,但我认为应该做一些事情,例如扩展 Layout,而不是将 Layout 添加到 Widget > 类使代码更简单。我还使用 kv 来设置 Home 类的成员。我将大按钮的宽度设置为其纹理(文本)的宽度,以将文本保留在按钮内。

无论如何,这是我的代码版本,我认为它可以满足您的需求。我使用 on_size 方法(在大小更改时调用)来重新排列按钮:

from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.app import App

Builder.load_string('''
#:set BUTTON_WIDTH 0.1
#:set BUTTON_HEIGHT 0.1
<Home>:
    Button:
        id: b1
        text: '1'
        size_hint: (BUTTON_WIDTH, BUTTON_HEIGHT)
        pos_hint: {'x':0, 'center_y': 0.5}
    Button:
        id: big
        text: 'big button'
        size_hint: (None, BUTTON_HEIGHT)
        size_x: self.texture_size[0]
        pos_hint: {'center_x':0.5, 'center_y': 0.5}
    Button:
        id: b2
        text: '2'
        size_hint: (BUTTON_WIDTH, BUTTON_HEIGHT)
        pos_hint: {'right':1.0, 'center_y': 0.5}
''')


class Home(FloatLayout):
    def __init__(self, **kwargs):
        super(Home, self).__init__(**kwargs)
        self.pos_switched = False  # keeps track of whether things have been re-arranged

    def on_size(self, instance, new_size):
        if not self.pos_switched and instance.width <= self.ids.b1.width + self.ids.big.width + self.ids.b2.width:
            # not enough room for the buttons side-by-side, re-arrange
            self.ids.b1.pos_hint = {'center_x': 0.5}
            self.ids.b1.y = self.ids.big.y + self.ids.big.height
            self.ids.b2.pos_hint = {'center_x': 0.5}
            self.ids.b2.y = self.ids.big.y - self.ids.b2.height
            self.pos_switched = True
        elif self.pos_switched and instance.width > self.ids.b1.width + self.ids.big.width + self.ids.b2.width:
            # ok to move the buttons back to original positions
            self.ids.b1.pos_hint = {'x':0, 'center_y': 0.5}
            self.ids.b2.pos_hint = {'right':1.0, 'center_y': 0.5}
            self.pos_switched = False



class TestApp(App):
    def build(self):
        return Home()


if __name__ == '__main__':
    TestApp().run()

关于python - 用 Kivy 居中和包裹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53053667/

相关文章:

python - factory_boy 工厂的多重继承——似乎只继承了第一个父级

python - Pylint 说 'string' 模块已弃用。获取小写字符范围的新方法是什么?

python-3.x - Pandas Groupby 的分类特征需要太多的 RAM 和时间

python-3.x - 如何使用kv语言引用kivy中创建的不同屏幕的小部件?

android - 在 kivy 的 ScrollView 中将文本与 Label 的边缘对齐

python - 使用 UDF 加入 Pyspark Dataframe

python - Google 表格 API "update"方法 Http 错误 400

python - 有没有一种方法可以根据 Pandas 中的键将数据有效地拆分成列

python - 检查给定数字是否为回文式的优化方法

python - 使 Kivy TextInput Frames 不可见但显示文本