📜  Python|将标签添加到 kivy 窗口

📅  最后修改于: 2022-05-13 01:54:40.203000             🧑  作者: Mango

Python|将标签添加到 kivy 窗口

Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、iOS、Linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。
标签小部件 -
Label 小部件用于呈现文本。它支持 ASCII 和 unicode字符串。标签是我们要添加到窗口中的文本,给按钮,等等。在标签上,我们也可以应用样式,即增加文本、大小、颜色等。
让我们看看如何将标签添加到 Kivy 窗口。

如何添加标签?

1) import kivy
2) import kivy App
3) import label
4) set minimum version (optional)
5) Extend the App class
6) overwrite the build function
7) Add and return label
8) Run the instance of class

下面是代码:

Python3
# import kivy module
import kivy
 
# this restricts the kivy version i.e
# below this kivy version you cannot use the app or software
kivy.require("1.9.1")
 
# 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 MyLabelApp(App):
    def build(self):
        # label display the text on screen
        lbl = Label(text ="Label is Added on screen !!:):)")
        return lbl
 
# creating the object
label = MyLabelApp()
# run the window
label.run()


Python3
# change only line 19 else all will same.
 
# text colour
l2 = Label(text ="Label is Added on \n screen !!:):)
            and its Multi\nLine", font_size ='20sp',
            color =[0.41, 0.42, 0.74, 1])


Python3
# markup text with different colour
l2 = Label(text ="[color = ff3333][b]'Label'[/b] is Added [/color]\n
                  [color = 3333ff]Screen !!:):):):)[/color]",
                  font_size ='20sp', markup = True)


Python3
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
from kivymd.uix.screen import Screen
 
class Demo(MDApp):
 
    def build(self):
        #defining screen
        screen = Screen()
 
        #defining 1st label
        l=MDLabel(text="Welcome!",pos_hint={'center_x':0.8,
                                            'center_y':0.8},
                  theme_text_color="Custom",
                  text_color=(0.5,0,0.5,1),
                  font_style='Caption')
         
        #defining 2nd label
        l1 = MDLabel(text="Welcome!", pos_hint={'center_x':0.8,
                                                'center_y':0.5},
                     theme_text_color="Custom",
                     text_color=(0.5, 0, 0.5, 1),
                     font_style='H2')
         
        #defining 3rd label
        l2 = MDLabel(text="Welcome!", pos_hint={'center_x':0.8,
                                                'center_y':0.2},
                     theme_text_color="Custom",
                     text_color=(0.5, 0, 0.5, 1),
                      font_style='H1')
         
        screen.add_widget(l)
 
 
        screen.add_widget(l1)
        screen.add_widget(l2)
        return screen
 
if __name__ == "__main__":
    Demo().run()


输出:


如何在标签中进行样式设置?

Python3

# change only line 19 else all will same.
 
# text colour
l2 = Label(text ="Label is Added on \n screen !!:):)
            and its Multi\nLine", font_size ='20sp',
            color =[0.41, 0.42, 0.74, 1])

输出:


如何标记文本?
您可以使用文本标记更改文本的样式。语法类似于上面的语法,但还有更多内容。

Python3

# markup text with different colour
l2 = Label(text ="[color = ff3333][b]'Label'[/b] is Added [/color]\n
                  [color = 3333ff]Screen !!:):):):)[/color]",
                  font_size ='20sp', markup = True)

输出:

我们可以使用更多的标记标签——

参考:https://kivy.org/doc/stable/api-kivy.uix.label.html

使用 KivyMD 标记
KivyMD 是 Kivy 框架的扩展。 KivyMD 是一组用于制作移动应用程序的 GUI 框架 Kivy 的 Material Design 小部件。

首先,我们将从 kivymd.uix.label 库中导入 MDLabel

MDLabel 具有以下参数-

  • text- 我们要放在标签上的文本
  • halign-我们要放置标签的位置。
  • theme_text_color - 自定义、主要、次要、提示或错误等文本颜色的主题
  • text_color - 如果 theme_text_color 是自定义的,我们可以将文本颜色分配给 RGB 元组。
  • font_style-like 标题,标题

下面是使用 MDLabel 的示例

Python3

from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
from kivymd.uix.screen import Screen
 
class Demo(MDApp):
 
    def build(self):
        #defining screen
        screen = Screen()
 
        #defining 1st label
        l=MDLabel(text="Welcome!",pos_hint={'center_x':0.8,
                                            'center_y':0.8},
                  theme_text_color="Custom",
                  text_color=(0.5,0,0.5,1),
                  font_style='Caption')
         
        #defining 2nd label
        l1 = MDLabel(text="Welcome!", pos_hint={'center_x':0.8,
                                                'center_y':0.5},
                     theme_text_color="Custom",
                     text_color=(0.5, 0, 0.5, 1),
                     font_style='H2')
         
        #defining 3rd label
        l2 = MDLabel(text="Welcome!", pos_hint={'center_x':0.8,
                                                'center_y':0.2},
                     theme_text_color="Custom",
                     text_color=(0.5, 0, 0.5, 1),
                      font_style='H1')
         
        screen.add_widget(l)
 
 
        screen.add_widget(l1)
        screen.add_widget(l2)
        return screen
 
if __name__ == "__main__":
    Demo().run()