内容表

上一话题

QLCDNumber

下一话题

QLayout

QLabel

QLabel widget provides a text or image display. 更多

Inheritance diagram of PySide2.QtWidgets.QLabel

概要

信号

详细描述

../../_images/windows-label.png

QLabel is used for displaying text or an image. No user interaction functionality is provided. The visual appearance of the label can be configured in various ways, and it can be used for specifying a focus mnemonic key for another widget.

A QLabel can contain any of the following content types:

内容

设置

纯文本

传递 QString to setText() .

富文本

传递 QString 包含富文本到 setText() .

像素图

传递 QPixmap to setPixmap() .

影片

传递 QMovie to setMovie() .

数字

传递 int double to setNum() , which converts the number to plain text.

什么都没有

如同空纯文本。这是默认。设置通过 clear() .

警告

当传递 QString 到构造函数或调用 setText() , make sure to sanitize your input, as QLabel tries to guess whether it displays the text as plain text or as rich text, a subset of HTML 4 markup. You may want to call setTextFormat() explicitly, e.g. in case you expect the text to be in plain format but cannot control the text source (for instance when displaying data loaded from the Web).

当使用这些函数中的任一改变内容时,任何先前内容被清零。

默认情况下,标签显示 左对齐,垂直居中 文本和图像,要显示文本中的任何 Tab 都会 automatically expanded . However, the look of a QLabel can be adjusted and fine-tuned in several ways.

The positioning of the content within the QLabel widget area can be tuned with setAlignment() and setIndent() . Text content can also wrap lines along word boundaries with setWordWrap() . For example, this code sets up a sunken panel with a two-line text in the bottom right corner (both lines being flush with the right side of the label):

label = QLabel(self)
label.setFrameStyle(QFrame.Panel | QFrame.Sunken)
label.setText("first line\nsecond line")
label.setAlignment(Qt.AlignBottom | Qt.AlignRight)
											

The properties and functions QLabel 继承自 QFrame 还可以用于指定要用于任何给定标签的 Widget 框架。

A QLabel is often used as a label for an interactive widget. For this use QLabel provides a useful mechanism for adding an mnemonic (see QKeySequence ) that will set the keyboard focus to the other widget (called the QLabel ‘s “buddy”). For example:

phoneEdit = QLineEdit(self)
phoneLabel = QLabel("&Phone:", self)
phoneLabel.setBuddy(phoneEdit)
											

In this example, keyboard focus is transferred to the label’s buddy (the QLineEdit ) 当用户按下 Alt+P 键时。若好友是按钮 (继承自 QAbstractButton ),触发助记键将模拟按钮点击。

class QLabel ( [ parent=None [ , f=Qt.WindowFlags() ] ] )

QLabel(text[, parent=None[, f=Qt.WindowFlags()]])

param f

WindowFlags

param parent

QWidget

param text

unicode

构造空标签。

parent 和 Widget 标志 f ,自变量被传递给 QFrame 构造函数。

构造标签显示文本 text .

parent 和 Widget 标志 f ,自变量被传递给 QFrame 构造函数。

PySide2.QtWidgets.QLabel. alignment ( )
返回类型

Alignment

另请参阅

setAlignment()

PySide2.QtWidgets.QLabel. buddy ( )
返回类型

QWidget

Returns this label’s buddy, or nullptr if no buddy is currently set.

另请参阅

setBuddy()

PySide2.QtWidgets.QLabel. clear ( )

清零任何标签内容。

PySide2.QtWidgets.QLabel. hasScaledContents ( )
返回类型

bool

PySide2.QtWidgets.QLabel. hasSelectedText ( )
返回类型

bool

PySide2.QtWidgets.QLabel. indent ( )
返回类型

int

另请参阅

setIndent()

PySide2.QtWidgets.QLabel. linkActivated ( link )
参数

link – unicode

PySide2.QtWidgets.QLabel. linkHovered ( link )
参数

link – unicode

PySide2.QtWidgets.QLabel. margin ( )
返回类型

int

另请参阅

setMargin()

PySide2.QtWidgets.QLabel. movie ( )
返回类型

QMovie

Returns a pointer to the label’s movie, or nullptr if no movie has been set.

另请参阅

setMovie()

返回类型

bool

PySide2.QtWidgets.QLabel. picture ( )
返回类型

QPicture

注意

此函数被弃用。

New code should use the other overload which returns QPicture by-value.

This function returns the label’s picture or nullptr if the label doesn’t have a picture.

另请参阅

setPicture()

PySide2.QtWidgets.QLabel. pixmap ( )
返回类型

QPixmap

注意

此函数被弃用。

New code should use the other overload which returns QPixmap by-value.

另请参阅

setPixmap()

PySide2.QtWidgets.QLabel. selectedText ( )
返回类型

unicode

PySide2.QtWidgets.QLabel. selectionStart ( )
返回类型

int

returns the index of the first selected character in the label or -1 if no text is selected.

注意

textInteractionFlags 设置标签需要包括 TextSelectableByMouse 或 TextSelectableByKeyboard。

另请参阅

selectedText()

PySide2.QtWidgets.QLabel. setAlignment ( arg__1 )
参数

arg__1 Alignment

另请参阅

alignment()

PySide2.QtWidgets.QLabel. setBuddy ( arg__1 )
参数

arg__1 QWidget

Sets this label’s buddy to buddy .

When the user presses the shortcut key indicated by this label, the keyboard focus is transferred to the label’s buddy widget.

The buddy mechanism is only available for QLabels that contain text in which one character is prefixed with an ampersand, ‘&’. This character is set as the shortcut key. See the mnemonic() documentation for details (to display an actual ampersand, use ‘&&’).

In a dialog, you might create two data entry widgets and a label for each, and set up the geometry layout so each label is just to the left of its data entry widget (its “buddy”), for example:

nameEd  = QLineEdit(self)
nameLb  = QLabel("&Name:", self)
nameLb.setBuddy(nameEd)
phoneEd = QLineEdit(self)
phoneLb = QLabel("&Phone:", self)
phoneLb.setBuddy(phoneEd)
# (layout setup not shown)
											

With the code above, the focus jumps to the Name field when the user presses Alt+N, and to the Phone field when the user presses Alt+P.

要取消先前设置的好友,调用此函数采用 buddy 设为 nullptr。

PySide2.QtWidgets.QLabel. setIndent ( arg__1 )
参数

arg__1 int

另请参阅

indent()

PySide2.QtWidgets.QLabel. setMargin ( arg__1 )
参数

arg__1 int

另请参阅

margin()

PySide2.QtWidgets.QLabel. setMovie ( movie )
参数

movie QMovie

将标签内容设为 movie . Any previous content is cleared. The label does NOT take ownership of the movie.

好友快捷方式 (若有的话) 被禁用。

PySide2.QtWidgets.QLabel. setNum ( arg__1 )
参数

arg__1 double

这是重载函数。

Sets the label contents to plain text containing the textual representation of double num . Any previous content is cleared. Does nothing if the double’s string representation is the same as the current contents of the label.

好友快捷方式 (若有的话) 被禁用。

另请参阅

setText() setNum() setBuddy()

PySide2.QtWidgets.QLabel. setNum ( arg__1 )
参数

arg__1 int

Sets the label contents to plain text containing the textual representation of integer num . Any previous content is cleared. Does nothing if the integer’s string representation is the same as the current contents of the label.

好友快捷方式 (若有的话) 被禁用。

另请参阅

setText() setNum() setBuddy()

参数

open bool

PySide2.QtWidgets.QLabel. setPicture ( arg__1 )
参数

arg__1 QPicture

将标签内容设为 picture 。任何先前内容被清零。

好友快捷方式 (若有的话) 被禁用。

PySide2.QtWidgets.QLabel. setPixmap ( arg__1 )
参数

arg__1 QPixmap

另请参阅

pixmap()

PySide2.QtWidgets.QLabel. setScaledContents ( arg__1 )
参数

arg__1 bool

PySide2.QtWidgets.QLabel. setSelection ( arg__1 , arg__2 )
参数
  • arg__1 int

  • arg__2 int

选择文本从位置 start 和对于 length 字符。

注意

textInteractionFlags 设置标签需要包括 TextSelectableByMouse 或 TextSelectableByKeyboard。

另请参阅

selectedText()

PySide2.QtWidgets.QLabel. setText ( arg__1 )
参数

arg__1 – unicode

另请参阅

text()

PySide2.QtWidgets.QLabel. setTextFormat ( arg__1 )
参数

arg__1 TextFormat

另请参阅

textFormat()

PySide2.QtWidgets.QLabel. setTextInteractionFlags ( flags )
参数

flags TextInteractionFlags

PySide2.QtWidgets.QLabel. setWordWrap ( on )
参数

on bool

另请参阅

wordWrap()

PySide2.QtWidgets.QLabel. text ( )
返回类型

unicode

另请参阅

setText()

PySide2.QtWidgets.QLabel. textFormat ( )
返回类型

TextFormat

另请参阅

setTextFormat()

PySide2.QtWidgets.QLabel. textInteractionFlags ( )
返回类型

TextInteractionFlags

PySide2.QtWidgets.QLabel. wordWrap ( )
返回类型

bool

另请参阅

setWordWrap()