Jam*_*s_L 6 python properties font-size kivy
无论是通过python还是kivy语言,在kivy中设置全局字体大小(即按钮和标签)的首选方法是什么?
根据窗口大小动态更改全局字体大小设置的好方法是什么?
qua*_*non 11
<Label>:
font_size: dp(20)
font_name: 'path/to/funcy/font.ttf'
Run Code Online (Sandbox Code Playgroud)
将为使用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()
Run Code Online (Sandbox Code Playgroud)
要使字体大小取决于屏幕大小,请不要使用固定值,而是使用以下方法计算它self.heigh:
[MyLabel@Label]:
text: ctx.text if hasattr(ctx, 'text') else ''
font_size: self.height/2
markup: True
Run Code Online (Sandbox Code Playgroud)
更新
另一种方法是使用 #: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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5129 次 |
| 最近记录: |