在python代码中设置标签类的text_size [英] Setting the text_size of label class in python code

查看:502
本文介绍了在python代码中设置标签类的text_size的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用以下代码调用的ListView小部件:

I have a ListView widget which is called using the following code:

.kv:

    ListView:
        id: myListView
        size_hint_y: 0.5

.py:

from kivy.adapters.simplelistadapter import SimpleListAdapter
from kivy.uix.label import Label

class CustomLabel(Label):
    font_name = 'fonts/DroidSansMono.ttf'
    font_size = 18
    color = (1, 1, 0.8, 1)
    halign = 'left'
    text_size = self.size # this line doesn't work


myList = ['item 1', 'item 2', 'item 3']
sla = SimpleListAdapter(data=myList, cls=CustomLabel)

我找不到设置文本大小的方法,因此它占用了标签小部件的整个尺寸. "self"不起作用,引用窗口小部件的ID也不起作用,因为它会在创建窗口小部件之前尝试在初始运行时解析代码.

I cannot find a way of setting the text size so it takes up the whole of the label widget's dimensions. 'self' doesn't work, and neither does referencing the widget's id, as it tries to parse the code at initial run time, before the widgets are created.

我应该怎么做?

推荐答案

self在类定义中不存在.但是您可以创建一条规则:

self does not exist in the class definition. But you can create a kivy rule:

ListView:
    id: myListView
    size_hint_y: 0.5
    text_size: self.size

这篇关于在python代码中设置标签类的text_size的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆