python - 如果窗口大小不够大,如何在 kivy 中使标签转到下一行

标签 python android user-interface kivy label

'''

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput

class MyGrid(GridLayout):
    def __init__(self, **kwargs):# handle as many can com in **kwargs
        super(MyGrid, self).__init__(**kwargs)
        self.cols = 1
        self.add_widget(Label(text="Super long textSuper long textSuper long textSuper long textSuper long textSuper long textSuper long textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper long text"))
class MyApp(App):
    def build(self):
        return MyGrid()

if __name__ == "__main__":
    MyApp().run()

'''

上面是我的代码,当我运行代码时,文本出现在窗口之外: enter image description here 那么,如果文本穿过窗口几何形状,有什么方法可以将文本移动到下一行。

任何建议都会有所帮助,谢谢。

最佳答案

您可以使用text_size属性,如下所示:

self.add_widget(Label(text_size=(500, None), text="Super long textSuper ...

另一种方法是使用 kivy 语言设置绑定(bind),随着窗口大小的变化调整 text_size:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout

kv = '''
<MyLabel>:
    size_hint: 0.5, 0.5
    text_size: self.size
'''

class MyLabel(Label):
    pass


class MyGrid(GridLayout):
    def __init__(self, **kwargs):# handle as many can com in **kwargs
        super(MyGrid, self).__init__(**kwargs)
        self.cols = 1
        self.add_widget(MyLabel(text="Super long textSuper long textSuper long textSuper long textSuper long textSuper long textSuper long textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper textSuper long text"))


class MyApp(App):
    def build(self):
        Builder.load_string(kv)
        return MyGrid()

if __name__ == "__main__":
    MyApp().run()

此代码将 text_size 设置为始终遵循 MyLabel 类中的小部件 size。使用 kv 设置绑定(bind),以便 text_size 将随着小部件 size 的变化而变化。

关于python - 如果窗口大小不够大,如何在 kivy 中使标签转到下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70601489/

相关文章:

android - Android在YouTubeAndroidPlayerAPI中出现问题。

java - 为什么我的SQL数据库无法在Android中使用?

java - 按钮 在 Java GUI (Swing) 中将鼠标放在按钮上后出现吗?

JavaFX - 当用户将鼠标从一个节点拖动到另一个节点时通知?

JavaFx tableview 排序真的很慢如何提高 java swing 中的排序速度

python - 如何在 python 中识别属于 numpy 数组中的集合的元素

python - 压缩 pandas DataFrame 以具有非空值并修改列名

android - 蓝牙连接被安全 rfcomm 套接字拒绝(适用于不安全)

python - 可以在 Azure HDInsight 中使用 Hadoop Streaming API 运行 python 代码吗?

python - 导入 Pandas 时缺少依赖项(numpy)