在kivy中将项目动态添加到滚动视图时发生font_size错误



你好,我是kivy的新手。我正在制作一个天气应用程序,我试图使用py文件中的for循环,每小时添加多个横幅(MDBoxLayout(,而不是在kv文件中创建24个MDBoxLayout。所以我在python文件中创建了hourly_banner类,但问题是font_size没有任何影响,我不知道如何更改所有内容的大小以使其适合。我有下面的对比图。感谢您的帮助,谢谢!

py文件

class hourly_banner(MDBoxLayout):
def __init__(self, **kwargs):
self.adaptive_height = True
self.orientation = "horizontal"
super(hourly_banner, self).__init__()
with self.canvas:
self.rect = Rectangle(pos=self.pos, size=self.size)
self.bind(pos=self.update_rect, size=self.update_rect)

time_label = MDLabel(size_hint_x=0.15, text=kwargs['time'], font_size= self.height*0.12)
weather_icon = MDIcon(size_hint_x=0.1, icon=kwargs['weather_icon'], font_size=self.height*0.12)
temp_label = MDLabel(size_hint_x=0.14, text=kwargs['temp'], font_size= self.height*0.15)
weather_code_label= MDLabel(size_hint_x=0.5, text=kwargs['weather_code'], font_size=self.height*0.125)
precip_layout = MDBoxLayout(adaptive_height=True, size_hint_x=0.2)
precip_layout_icon = MDIcon(icon=kwargs['precip_icon'], font_size=self.height*0.15)
precip_layout_label = MDLabel(text=kwargs['precip_percent'], font_size=self.height*0.125)
precip_layout.add_widget(precip_layout_icon)
precip_layout.add_widget(precip_layout_label)
self.add_widget(time_label)
self.add_widget(weather_icon)
self.add_widget(temp_label)
self.add_widget(weather_code_label)
self.add_widget(precip_layout)
class MyApp(MDApp):

def build(self):
self.theme_cls.primary_palette = "LightBlue"
self.theme_cls.primary_hue = "700"
self.theme_cls.theme_style = "Dark"
return Builder.load_file("main.kv")

def on_start(self):
self.menu_1 = self.create_menu(
"Button menu", self.root.ids['site_screen'].ids['toolbar'].ids['button_1'])
self.menu_2 = self.create_menu(
"Button dots", self.root.ids['site_screen'].ids['toolbar'].ids['button_2'])
hourly_grid = self.root.ids["site_screen"].ids['hourly_grid']
for i in range(20):
H = hourly_banner(time='12PM', weather_icon="weather-sun", temp="48", weather_code="Mostly Coudly",
precip_icon='water-percent', precip_percent="38%")
hourly_grid.add_widget(H)

kv文件:

MDBottomNavigationItem:
id: second_icon
on_leave:
second_icon.icon = "clock-time-four-outline"
on_tab_press:
second_icon.icon = "clock-time-four"
#toolbar.title = "Second"
toolbar.label.text = "Second"
#text: "Forecast"
name: "site_hourly_screen"
icon: "clock-time-four-outline"
#Content
MDScrollViewRefreshLayout:
id: refresh_layout_hourly
refresh_callback: app.refresh_callback_hourly
root_layout: root
pos_hint: {"center_x": .5}
size_hint: 0.9, 1
#padding: 10
MDGridLayout:
id: hourly_grid
cols: 1
adaptive_height: True
padding: 0, "20dp"
row_default_height: "50dp"
row_force_default: True
MDBoxLayout:
adaptive_height: True
orientation: 'horizontal'
MDLabel:
size_hint_x: 0.15
text: "12PM"
font_size: self.height*0.12
MDIcon:
size_hint_x: 0.1
icon: "weather-sunny"
font_size: self.height*0.15
MDLabel:
size_hint_x: 0.14
text: "48"
font_size: self.height*0.125
MDLabel:
size_hint_x: 0.5
text: "freezing_rain_heavy"
font_size: self.height * 0.125
MDBoxLayout:
adaptive_height: True
size_hint_x: 0.2
MDIcon:
icon: "water-percent"
font_size: self.height*0.15
MDLabel:
text: "38%"
font_size: self.height*0.125
MDBoxLayout:
adaptive_height: True
orientation: 'horizontal'
MDLabel:
size_hint_x: 0.15
text: "12PM"
font_size: self.height*0.12
MDIcon:
size_hint_x: 0.1
icon: "weather-sunny"
font_size: self.height*0.15
MDLabel:
size_hint_x: 0.145
text: "48"
font_size: self.height*0.125
MDLabel:
size_hint_x: 0.45
text: "freezing_rain_heavy"
font_size: self.height * 0.125
MDBoxLayout:
adaptive_height: True
size_hint_x: 0.2
MDIcon:
icon: "water-percent"
font_size: self.height*0.15
MDLabel:
text: "38%"
font_size: self.height*0.125
MDBoxLayout:
adaptive_height: True
orientation: 'horizontal'
MDLabel:
size_hint_x: 0.15
text: "12PM"
font_size: self.height*0.12
MDIcon:
size_hint_x: 0.1
icon: "weather-sunny"
font_size: self.height*0.15
MDLabel:
size_hint_x: 0.145
text: "48"
font_size: self.height*0.125
MDLabel:
size_hint_x: 0.45
text: "freezing_rain_heavy"
font_size: self.height * 0.125
MDBoxLayout:
adaptive_height: True
size_hint_x: 0.2
MDIcon:
icon: "water-percent"
font_size: self.height*0.15
MDLabel:
text: "38%"
font_size: self.height*0.125

比较图像:从kv实现与从python 实现

这是的问题

time_label = MDLabel(size_hint_x=0.15, text=kwargs['time'], font_size= self.height*0.12)

在这一行中,自行参考MDBoxLayout,而不是像在kv文件中那样参考标签这是指MDLabel,这就是为什么的字体大小不同的原因

MDLabel:
size_hint_x: 0.15
text: "12PM"
font_size: self.height*0.12 

解决方案是在python文件中使用自定义MDRabel在python文件中添加此

class CustomMDLabel(MDLabel):
pass

在kv文件中

<CustomMDLabel>: 
size_hint_x: 0.15
font_size: self.height*0.12

并在hourly_anner类中使用它,而不是在普通的MDLabel中使用像这个

time_label = CustomMDLabel(text=kwargs['time'])

最新更新