我希望我创建的CustomLabels和CustomToggleButtons可以根据不同的Window大小更改其大小。所以这就是我所做的。
lbl = CustomLabel(文本=文本,宽度= self.grid_layout.width,> height = 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,然后是小部件。
但是,它没有用。每当我更改窗口大小时,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"]
}
答案 0 :(得分:0)
我认为最简单的解决方法是使用size_hint
。尝试将CustomLabel
中的CustomTogglebutton
和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