【发布时间】:2021-06-27 17:34:46
【问题描述】:
所以我尝试解决我的问题:
我有两个屏幕,当我从一个屏幕切换到另一个屏幕时,它应该在第二个屏幕上动态生成按钮(id:“thema_screen”)。这些按钮在第二个屏幕上创建为 GridLayout,其 id 为:“grid_thema_screen”
现在,如果我更改窗口大小,我希望按钮的 font_size 自动更改,这与 kivy-file 中的font_size: min(root.height, root.width) / 10 效果很好,但是当我将其写入 python 文件 add_widget(Button(text = str(i+2), font_size = min(self.root.height, self.root.width) / 2))
它改变了一次字体大小,但是如果我重新调整窗口,字体就不再改变了。这是我的python文件的代码sn-p:
class Wurzel(MDBoxLayout):
def __init__(self, **kwargs):
super(Wurzel, self).__init__(**kwargs)
def changeScreen(self, next_screen):
if next_screen == "thema_screen":
self.ids.wurzel_screen_manager.current = next_screen
for i in range(12):
self.ids.thema_screen.ids.grid_thema_screen.add_widget(Button(text = str(i+2), font_size = min(self.root.height,
self.root.width) / 10))
这里是我的 kivy 文件:
<Wurzel>:
orientation: 'vertical'
spacing: 20
MDToolbar:
title: 'Hellllooo'
left_action_items: [["menu", lambda x: app.navigation_klick()], ["apple", lambda x: app.navigation_klick()]]
elevation: 8
ScreenManager:
id: wurzel_screen_manager
StartScreen:
name: "start_screen"
ThemaScreen:
id: thema_screen
name: "thema_screen"
<StartScreen@MDScreen>:
BoxLayout:
orientation: "vertical"
...
<ThemaScreen@MDScreen>:
GridLayout:
id: grid_thema_screen
cols: 3
padding: root.width * .01, root.height * .01
spacing: min(root.width, root.height) * .02
Button:
text: "1"
font_size: min(root.height, root.width) / 10
size_hint: None, None
size: self.font_size*3, self.font_size*3
所以,如果有人可以帮助我并告诉我,如果我在 Python 中动态创建按钮,为什么字体大小不再自动更改,那就太好了。 非常感谢。
【问题讨论】: