如何制作kivy标签多行文本?
Kivy 是一个开源软件库,用于快速开发配备新颖用户界面的应用程序,例如多点触控应用程序。在您的计算机上使用 Kivy,您可以创建在以下平台上运行的应用程序:
- 台式计算机:OS X、Linux、Windows。
- IOS 设备:iPad、iPhone。
- Android 设备:平板电脑、手机。
- 支持 TUIO(有形用户界面对象)的任何其他支持触控的专业/家庭酿造设备。
Kivy 中的标签
Label 小部件用于呈现文本。它支持 ascii 和 unicode字符串。标签是我们想要添加到窗口上的文本,给按钮等等。在标签上,我们也可以应用样式,即增加文本、大小、颜色等。
程序
- 使用cmd命令“pip install kivy”在你的电脑上安装kivy
- 导入 kivy 及其 App 模块,如下例所示
- 创建一个继承App模块的类
- 在类中定义一个build方法,在这个方法中定义你要创建的标签,然后返回标签
- 为类创建一个对象
- 最后对对象使用 run() 命令
普通标签代码
Python3
# make sure you have installed kivy for this to work
import kivy
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
# if you not import label and use it it through error
from kivy.uix.label import Label
# defining the App class
class MyDemoApp(App):
def build(self):
# label display the text on screen
ll = Label(text="This is a normal label")
return ll
# creating the object
label = MyDemoApp()
# run the window
label.run()
Python3
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyDemoApp(App):
def build(self):
ll = Label(text="This is a\nmultiline\nlabel")
return ll
label = MyDemoApp()
label.run()
Python3
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyDemoApp(App):
def build(self):
ll = Label(text="GeeksForGeeks is the \nbest platform for \nDSA content")
return ll
label = MyDemoApp()
label.run()
Python3
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyDemoApp(App):
def build(self):
ll = Label(text='''Kivy is an open source software library
\nfor the rapid development of applications
\nequipped with novel user interfaces,
\nsuch as multi-touch apps.''')
return ll
label = MyDemoApp()
label.run()
输出 :
多行标签代码
您可以在每行末尾使用 '\n' 轻松地使文本多行。
示例 1:
蟒蛇3
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyDemoApp(App):
def build(self):
ll = Label(text="This is a\nmultiline\nlabel")
return ll
label = MyDemoApp()
label.run()
输出 :
示例 2:
蟒蛇3
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyDemoApp(App):
def build(self):
ll = Label(text="GeeksForGeeks is the \nbest platform for \nDSA content")
return ll
label = MyDemoApp()
label.run()
输出 :
示例 3:
蟒蛇3
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyDemoApp(App):
def build(self):
ll = Label(text='''Kivy is an open source software library
\nfor the rapid development of applications
\nequipped with novel user interfaces,
\nsuch as multi-touch apps.''')
return ll
label = MyDemoApp()
label.run()
输出 :
注意:有关 kivy 的更多信息,您可以使用此链接访问官方文档。