0

我希望我创建的 CustomLabels 和 CustomToggleButtons 根据不同的窗口大小来改变它们的大小。所以这就是我所做的。

lbl = CustomLabel(文本=文本,宽度=self.grid_layout.width,>高度=self.grid_layout.height*.5)

btn = CustomToggleButton(group=text, text=config_type[i], width=40 + >len(config_type[i]) * 20, height=self.grid_layout.height * .5)

我所做的是将上述小部件的高度和宽度设置为与包含它们的 GridLayout 成比例。GridLayout 的大小是相对于 MainScreen 的。因此,如果我更改 Window 大小,GridLayout 将紧随其后,然后是小部件。

但是,它没有用。每当我更改 Window 大小时,CustomLabels 和 ToggleButtons 的大小都会保持不变。将不胜感激一些帮助,谢谢!

这是完整的代码:

Builder.load_string('''

#:import hex kivy.utils.get_color_from_hex

<CustomButton>
    font_size: self.width * .1
    background_color: hex('#669999')
    
<CustomToggleButton>:
    background_color: hex('#669999')
    font_size: self.height*.5
    size_hint: None, None
    # height: self.parent.parent.height * .1
    
<CustomLabel>:
    font_size: self.height * .5
    size_hint: None, None
    text_size: self.size
    halign: 'left'
    # width: self.parent.parent.width
    # height: self.parent.parent.height * .1 

<CustomBoxLayout>:
    orientation: 'vertical'
    
<CustomStackLayout>:
    size_hint_y: None
    height: self.minimum_height
    spacing: 10

<TempBoxLayout>:
    spacing: 10
    CustomBoxLayout:
        spacing: 10
        CustomButton:
            text: 'Temp Up'
        CustomButton:
            text: 'Temp Down'
    Label:
        text: '20'
        font_size: self.width * .1
        canvas.before:
            Color: 
                rgba: hex('#000000')
            Rectangle:
                pos: self.pos
                size: self.size

<MainScreen>:
    grid_layout: grid_layout
    CustomBoxLayout:
        padding: 10
        spacing: 10
        canvas.before:
            Color:
                rgba: hex('#669999')
            Rectangle:
                size: self.size
                pos: self.pos
        GridLayout:
            id: grid_layout
            canvas.before:
                Color:
                    rgba: hex('#000000')
                Rectangle:
                    size: self.size
                    pos: self.pos
        BoxLayout:
            size_hint_y: .4
            BoxLayout:
                size_hint_x: .25
            TempBoxLayout:
            BoxLayout:
                size_hint_x: .25

''')


class CustomButton(Button):
    pass


class CustomToggleButton(ToggleButton):
    pass


class CustomLabel(Label):
    pass


class CustomBoxLayout(BoxLayout):
    pass


class CustomStackLayout(StackLayout):
    pass


class TempBoxLayout(BoxLayout):
    pass


class MainScreen(Screen):
    store = JsonStore('remote_config.json')
    power = str(store['power'][0])
    mode = str(store['modes'][0])
    fan_speed = str(store['fan_speed'][0])
    swing = str(store['swing'][0])
    louver = str(store['louver'][0])
    min_temp = store['min_temp']
    max_temp = store['max_temp']
    curr_temp = min_temp
    temp_interval = store['interval']
    config = [store['power'], store['modes'], store['fan_speed'], store['swing'], store['louver']]
    titles = ['Power', 'Mode', 'Fan', 'Swing', 'Louver']
    grid_layout = ObjectProperty()

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.grid_layout.rows = 0
        print(self.power, self.mode, self.fan_speed, self.swing, self.louver)
        for i in range(len(self.config)):
            self.populate_controls(self.config[i], self.titles[i])

    def populate_controls(self, config_type, text):
        if len(config_type) is not 1:
            if config_type != 'not available':
                stack_layout = CustomStackLayout()
                self.grid_layout.rows += 1
                lbl = CustomLabel(text=text, width=self.grid_layout.width, height=self.grid_layout.height*.5)
                stack_layout.add_widget(lbl)
                for i in range(len(config_type)):
                    btn = CustomToggleButton(group=text, text=config_type[i], width=40 + len(config_type[i]) * 20,
                                          height=self.grid_layout.height   * .5)
                    if i is 0:
                        btn.state = 'down'
                    stack_layout.add_widget(btn)
                self.grid_layout.add_widget(stack_layout)


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


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

这是 JSON 文件:

{
  "power": ["off", "on"],
  "min_temp": 18,
  "max_temp": 32,
  "interval": 1,
  "modes": ["cool", "fan", "dry"],
  "fan_speed": ["auto", "low", "med-low", "med", "med-high", "high"],
  "swing": ["off", "auto"],
  "louver": ["off", "auto"]
}
4

1 回答 1

0

我认为最简单的解决方法是使用size_hint. 尝试将您的CustomLabelCustomTogglebutton定义更改kv为:

<CustomToggleButton>:
    background_color: hex('#669999')
    font_size: self.height*.5
    size_hint: 0.1, 0.5

<CustomLabel>:
    font_size: self.height * .5
    size_hint: 0.1, 0.5
    text_size: self.size
    halign: 'left'

width而且您不再需要这些对象的代码height中的规范。Python另外,更改CustomStackLayout为:

<CustomStackLayout>:
    size_hint_y: 1.0
    spacing: 10
于 2018-07-05T02:41:05.117 回答