Kivy,有没有办法只更改布局中的所有按钮属性

2024-10-03 06:18:53 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有方法使用<;更改按钮属性;按钮>;但只是在一个特定的布局中

我不想在执行过程中更改大小,我只是不想为所有按钮编写它,只想在我的布局中编写一次

示例:在我下面的代码中,我想要更改MyLayout中所有按钮的大小,而不是LoadFilePicker中的按钮

科林

MyLayout:
    Button:
        pos_hint: {"x":0.05, "top":0.95}
        text:"Import config"
        on_release:
            root.loadPicker()

    Button:
        pos_hint: {"x":0.2, "top":0.9}
        text:"Poll A"

<LoadFilePicker>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:

                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)

下面是MyLayout类的定义

class MyLayout(FloatLayout):
    def load(self, path, filename):
        self.dismiss_popup()
        db = DataBase(filename)
        db.loadData()

    def dismiss_popup(self):
        self._popup.dismiss()

    def loadPicker(self):
        content = LoadFilePicker(load=self.load, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file",content=content,size_hint=(0.9, 0.9))
        self._popup.open()

Tags: textposselfsizereleaseonloadbutton
2条回答

有几种方法可以做到这一点,其中一种方法类似于MyLayout类中的以下方法:

for child in self.children:
    if isinstance(child, Button):
        child.size = 200, 100

您可以像这样子类化Button

<MyButton@Button>:
    size_hint: None, None
    size: 200, 100

然后在MyLayout中使用该子类:

MyLayout:
    MyButton:
        pos_hint: {"x":0.05, "top":0.95}
        text:"Import config"
        on_release:
            root.loadPicker()

    MyButton:
        pos_hint: {"x":0.2, "top":0.9}
        text:"Poll A"

相关问题 更多 >