【发布时间】:2020-02-23 23:12:56
【问题描述】:
我正在尝试在更改标签字体大小的设置中创建一个选项。
我尝试使用属性,以便当用户在设置中更改字体大小时,结果/更改将立即显示。相反,更改仅在重新启动应用程序时生效。这是我的代码供参考。
example.py
import kivy
from kivy.properties import *
from kivy.uix.settings import SettingsWithSidebar
from kivy.uix.label import Label
from kivy.config import ConfigParser
from kivy.app import App
example_config = ConfigParser()
example_config.read("example.ini")
class TitleLabel(Label):
font_size = ConfigParserProperty(defaultvalue=40, section="style", key="font_size",
config=example_config, rebind=True)
class ExampleApp(App):
def build(self):
self.settings_cls = SettingsWithSidebar
self.use_kivy_settings = False
def build_config(self, config):
config.setdefaults(
"style", {"font_size": 40}
)
def build_settings(self, settings):
settings.add_json_panel("Settings", self.config, "settings.json")
if __name__ == "__main__":
ExampleApp().run()
example.kv
FloatLayout:
TitleLabel:
SettingsButton:
<TitleLabel>:
text: "[color=#30C9E9]ExampleTitleText[/color]"
markup: True
font_size: int(root.font_size)
size_hint: None, None
size: self.texture_size
pos_hint: {"center_x": 0.5, "center_y": 0.5}
<SettingsButton@Button>:
text: "Settings"
size_hint: 0.1, 0.1
pos_hint: {"right": 0.975, "top": 0.975}
on_release: app.open_settings()
settings.ini
[
{
"type": "numeric",
"title": "Font Size",
"desc": "Pick how big you want the font to be.",
"section": "style",
"key": "font_size"
}
]
我尝试将ConfigParserProperty() 链接到font_size(在example.kv 下<TitleLabel>)。我也尝试将NumericProperty(example_config.get("style", "font_size")) 链接到font_size。都没有奏效。我知道它必须与 properties 相关,但我不知道如何设置它。
如果您知道如何解决此问题,我们将非常感谢您的帮助。
【问题讨论】:
标签: python kivy kivy-language