Python 将标签添加到Kivy窗口

2021年3月10日16:21:59 发表评论 705 次浏览

KivyPython中与平台无关的GUI工具。由于它可以在Android, IOS, Linux和Windows等操作系统上运行。它基本上是用于开发Android应用程序, 但这并不意味着它不能在桌面应用程序上使用。

标签小部件–

标签窗口小部件用于呈现文本。它支持ascii和unicode字符串。标签是我们要在窗口中添加, 提供给按钮等的文本。在标签上, 我们也可以应用样式, 即增加文本, 大小, 颜色等。

让我们看看如何将Label添加到基维窗口。

👉🏽Kivy教程–通过示例学习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

下面是代码:

# 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()

输出如下:

Python |将标签添加到Kivy窗口1

如何在标签中样式化?

# 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 ])

输出如下:

Python |将标签添加到Kivy窗口2

如何标记文字?

你可以使用文本标记来更改文本的样式。语法与上述语法相似, 但还有更多内容。

# 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 )

输出如下:

Python |将标签添加到Kivy窗口3

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

[b] [/ b]->激活粗体文本[i] [/ i]->激活斜体文本[u] [/ u]->带下划线的文本展开 [/ s]->删除线文本[font =] [/ font]->更改字体[size =] [/ size]]->更改字体大小[color =#] [/ color]->更改文字颜色[ref =] [/ ref]->添加交互式区域。引用内的引用+边界框将在Label.refs [anchor =]->在文本中放置锚点中提供。你可以使用Label.anchors [sub] [/ sub]->在相对于前一个文本的下标位置显示文本, 来获取锚在文本中的位置。 [sup] [/ sup]->在相对于之前的文本的上标位置显示文本。

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

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: