Thirty-one -QLabel study concluded GUI learning

In front of all the controls are attributed to the input control, here we want to show summary usage under control. To the first is the most commonly used QLabel.

I. Description

   Label control (the QLabel) provides the ability to display the text or image may be used to display

  Plain text

  digital

  Rich Text

  image

  Animated (GIF)

  and many more

  But he did not provide user interaction (but some time in the rich text hyperlinks are clickable)

  QLabel is inherited sub QFrame, can operate on the frame

II. Function

  Basic features we have used in the front, the rest of us to find out:

  1. Align

QLabel.setAlignment(self, a0: typing.Union[QtCore.Qt.Alignment, QtCore.Qt.AlignmentFlag])
QLabel.alignment() -> QtCore.Qt.Alignment
AlignLeft = ...  # type: 'Qt.AlignmentFlag'
AlignLeading = ...  # type: 'Qt.AlignmentFlag'
AlignRight = ...  # type: 'Qt.AlignmentFlag'
AlignTrailing = ...  # type: 'Qt.AlignmentFlag'
AlignHCenter = ...  # type: 'Qt.AlignmentFlag'
AlignJustify = ...  # type: 'Qt.AlignmentFlag'
AlignAbsolute = ...  # type: 'Qt.AlignmentFlag'
AlignHorizontal_Mask = ...  # type: 'Qt.AlignmentFlag'
AlignTop = ...  # type: 'Qt.AlignmentFlag'
AlignBottom = ...  # type: 'Qt.AlignmentFlag'
AlignVCenter = ...  # type: 'Qt.AlignmentFlag'
AlignVertical_Mask = ...  # type: 'Qt.AlignmentFlag'
AlignCenter = ...  # type: 'Qt.AlignmentFlag'
AlignBaseline = ...  # type: 'Qt.AlignmentFlag'
Align enumeration value

  2. indentation and margins

QLabel.setIndent (Self, A0: int) # indent 
QLabel.indent () -> int 
QLabel.setMargin (Self, A0: int) # on the lower left and right pitches reserved 
QLabel.margin () -> int

  3. Text Format

  The default setting is text QLabel rich text, rich text, if there is a direct follow in the rich text format in the initialization, if we want to display in plain text format, then we should set up his own.

QLabel.setTextFormat(self, a0: QtCore.Qt.TextFormat)
PlainText = ...  # type: 'Qt.TextFormat'
RichText = ...  # type: 'Qt.TextFormat'
AutoText = ...  # type: 'Qt.TextFormat'

  4. Set small partner

  In some cases this is the effect

  Each label corresponds to a QLineedit, Note label where there is a corresponding shortcut key, which is the role of junior partner settings, use the shortcut keys can complete the switch input text box

QLabel.setBuddy(self, a0: QWidget)

  Code dialog box similar to the above login below

from PyQt5.Qt import *
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(800,600)
        self.UI_test()


    def UI_test(self):
        label_1 = QLabel('账号&a',self)
        label_2 = QLabel('密码&b',self)

        le1 = QLineEdit(self)
        le2 = QLineEdit(self)

        label_1.move(100,100)
        label_2.move(100,150)

        le1.move(150,100)
        le2.move(150,150)

        label_1.setBuddy(le1)
        label_2.setBuddy(le2)
        pass
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
Small partners set

  5. Content Zoom

  Sometimes the label control to display the picture, but the image is too small when you need the image to zoom (not changing the size of the label)

QLabel.setScaledContents(self, a0: bool)

  Set after picture is scaled according to the size of the display after the QLabel.

  6. interactive text logo

QLabel.setTextInteractionFlags (Self, flags: typing.Union [QtCore.Qt.TextInteractionFlags, QtCore.Qt.TextInteractionFlag]) 
of the type: ' Qt.TextInteractionFlag ' 
NoTextInteraction = ...   # can not interact 
TextSelectableByMouse = ...   # can be selected with the mouse 
= ... TextSelectableByKeyboard   # can select the keyboard 
LinksAccessibleByMouse = ...   # can use the mouse to highlight and activation link 
LinksAccessibleByKeyboard = ...   # can use the tabs to focus and activate the link with the Enter 
TextEditable = ...   # editable 
TextEditorInteraction =. ..   # text editor defaults 
TextBrowserInteraction = ...   # The default value of QTextBrowser

  In fact, this is useless, because if we need to interact with the text and the like can be used QLineEdit control and no need to use this.

  7. Select the text

  And realizes the direct effect selected by the code

QLabel.setSelection (Self, A0: int, a1: int) # A0 is the initial position, a1 length 
QLabel.hasSelectedText () -> BOOL 
QLabel.selectedText () - > STR 
QLabel.selectionStart () - > int

  Ditto a little useful.

  8. External links

  Sometimes shown on the label is a hyperlink, but only allowed to set up external links can open the corresponding web page

QLabel.setOpenExternalLinks(self, open: bool)

  The following methods can open Baidu page, otherwise the arrow turns the mouse finger links, but later can not click open the corresponding web page

label = QLabel('<a href = www.baidu.com>百度</a>', self)
label.setOpenExternalLinks(True)

  9. Wrap

QLabel.setWordWrap(self, on: bool)

  Open after wrapping will wrap at the end of a word, a usage spots here: default label in the word are sideways row, if we want the word by way bristling display, built-in method does not work, we can do so

label = QLabel('\n'.join('123456789'),self)

  So out of effect is this (ignore the colors and fonts for display)

III. Signal

  As the general did not function interaction, QLabel control signal is relatively small, the most common is to click on a hyperlink

QLabel.linkHovered (Self, Link: str)    # mouse to hyperlink to 
QLabel.linkActivated (Self, Link: str) # mouse click on a hyperlink

  Signaling parameters hyperlink strings. One caveat: if the previous set allowed to open external link, click the hyperlink of the signal will not be triggered (pointing to still be triggered).

IV. Content operation

  Different data type setting methods are different, we do not have the conversion

# String 
QLabel.setText () 
QLabel.text () 
# Digital 
QLabel.setNum ()
 # Graph 
QLabel.setPicture (Self, A0: QtGui.QPicture)    
QLabel.setPixmap (Self, A0: QtGui.QPixmap) 
# movable FIG. 
QLabel.setMovie ()
 # empty all contents 
QLabel.clear ()
label = QLabel(self)
pic = QPicture()
painter = QPainter(pic)
painter.setBrush(QBrush(QColor(255,255,255)))
painter.drawEllipse(20,20,200,200)
label.setPicture(pic)
Set the image
label = QLabel(self)
movie = QMovie(r'C:\Users\Aaron\Desktop\test\249093-2.gif')
label.setMovie(movie)
movie.start()
FIG disposed movable

  Moving map in the QMovie there are a few ways to talk about the role of directly see the name to know

QMovie.setScaledSize(self, size: QtCore.QSize)
QMovie.setSpeed(self, percentSpeed: int)#播放速度
QMovie.setPaused(self, paused: bool)
QMovie.stop()
QMovie.start()

 

Guess you like

Origin www.cnblogs.com/yinsedeyinse/p/11607421.html