[英]Setting global font size in kivy
無論是通過python還是kivy語言,在kivy中設置全局字體大小(即按鈕和標簽)的首選方法是什么?
根據窗口大小動態更改全局字體大小設置的好方法是什么?
<Label>:
font_size: dp(20)
font_name: 'path/to/funcy/font.ttf'
將為使用Label作為基礎的任何窗口小部件設置全局字體名稱和字體大小(TextInput和其他一些小部件不會)。
使用模板創建自定義標簽:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty, NumericProperty
kv = '''
[MyLabel@Label]:
text: ctx.text if hasattr(ctx, 'text') else ''
font_size: 24
markup: True
<MyWidget>:
id: f_wid
BoxLayout:
size: f_wid.size
orientation: 'vertical'
MyLabel:
text: "Hello world 1"
MyLabel:
text: "Hello world 2"
MyLabel:
text: "Hello world 3"
MyLabel:
text: "Hello world 4"
MyLabel:
text: "Hello world 1"
MyLabel:
text: "Hello world 2"
MyLabel:
text: "Hello world 3"
MyLabel:
text: "Hello world 4"
'''
Builder.load_string(kv)
import kivy
kivy.require('1.7.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.widget import Widget
class MyWidget(Widget):
pass
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
要使字體大小取決於屏幕大小,而不是使用固定值,使用self.heigh
計算它:
[MyLabel@Label]:
text: ctx.text if hasattr(ctx, 'text') else ''
font_size: self.height/2
markup: True
UPDATE
替代方法是使用#:set語法設置變量:
kv = '''
#:set default_font_size "36sp"
<MyWidget>:
id: f_wid
BoxLayout:
size: f_wid.size
orientation: 'vertical'
Label:
text: "Hello world 1"
font_size: default_font_size
Label:
text: "Hello world 2"
font_size: default_font_size
Label:
text: "Hello world 3"
font_size: default_font_size
Label:
text: "Hello world 4"
font_size: default_font_size
Label:
text: "Hello world 1"
font_size: default_font_size
Label:
text: "Hello world 2"
font_size: default_font_size
Label:
text: "Hello world 3"
font_size: default_font_size
Label:
text: "Hello world 4"
font_size: default_font_size
'''
Builder.load_string(kv)
我知道這個問題很老但你確實問過“動態改變全局字體大小設置與窗口大小成比例”
對於類似的問題,我創建了AutoSizedLabel
class TestApp(App):
def build(self):
return AutoSizedLabel(text="crazy stuff", ratio=0.5)
它是pip安裝的:
pip install kivyoav
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.