PyQt5 tutorial "Controls Widgets II"

table of Contents

PyQt5 Control II

QPixmap

Input box (QLineEdit)

QSplitter

List (QComboBox)


PyQt5 Control II

Here, we will continue to introduce the PyQt5 widget. We will introduce the QPixmapQLineEdit, QSplitter, and QComboBox.

 

QPixmap

QPixmapis one of the widgets for processing images. It is optimized for displaying images on the screen. In our code example, we will use QPixmapto display the image on the window.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import (QWidget, QHBoxLayout, 
    QLabel, QApplication)
from PyQt5.QtGui import QPixmap
import sys

class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):      

        hbox = QHBoxLayout(self)
        pixmap = QPixmap("redrock.png")

        lbl = QLabel(self)
        lbl.setPixmap(pixmap)

        hbox.addWidget(lbl)
        self.setLayout(hbox)
        
        self.move(300, 200)
        self.setWindowTitle('Red Rock')
        self.show()        
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

In our example, we display the image on the window.

pixmap = QPixmap("redrock.png")

We create an QPixmapobject. It takes the name of the file as a parameter.

lbl = QLabel(self)
lbl.setPixmap(pixmap)

We put pixmap into the QLabelwidget.

Input box (QLineEdit)

QLineEditIs a widget that allows input and editing of single lines of plain text. You can provide undo and redo, cut and paste, and drag and drop functions for widgets.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import (QWidget, QLabel, 
    QLineEdit, QApplication)


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):      

        self.lbl = QLabel(self)
        qle = QLineEdit(self)
        
        qle.move(60, 100)
        self.lbl.move(60, 40)

        qle.textChanged[str].connect(self.onChanged)
        
        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('QLineEdit')
        self.show()
        
        
    def onChanged(self, text):
        
        self.lbl.setText(text)
        self.lbl.adjustSize()        
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

This example shows line editing widgets and labels. The text we typed in the line edit will immediately appear in the label widget.

qle = QLineEdit(self)

The QLineEditcontrol is created.

qle.textChanged[str].connect(self.onChanged)

If the text in the line editing widget changes, we will call this onChanged() method.

def onChanged(self, text):
    
    self.lbl.setText(text)
    self.lbl.adjustSize() 

Inside the onChanged()method, we set the typed text as the label widget. We call this adjustSize()method to adjust the size of the label to the length of the text.

QLineEdit

Figure: QLineEdit

QSplitter

QSplitterLet the user control the size of the child widget by dragging the border between the child windows. In our example, we show QFramethree widgets organized using two dividers.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame, 
    QSplitter, QStyleFactory, QApplication)
from PyQt5.QtCore import Qt
import sys

class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):      

        hbox = QHBoxLayout(self)

        topleft = QFrame(self)
        topleft.setFrameShape(QFrame.StyledPanel)
 
        topright = QFrame(self)
        topright.setFrameShape(QFrame.StyledPanel)

        bottom = QFrame(self)
        bottom.setFrameShape(QFrame.StyledPanel)

        splitter1 = QSplitter(Qt.Horizontal)
        splitter1.addWidget(topleft)
        splitter1.addWidget(topright)

        splitter2 = QSplitter(Qt.Vertical)
        splitter2.addWidget(splitter1)
        splitter2.addWidget(bottom)

        hbox.addWidget(splitter2)
        self.setLayout(hbox)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QSplitter')
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

In our example, we have three frame widgets and two splitters. Please note that under some topics, the splitter may not look clearly.

topleft = QFrame(self)
topleft.setFrameShape(QFrame.StyledPanel)

We use style frames to view QFramethe boundaries between widgets.

splitter1 = QSplitter(Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)

We create a QSplitterwidget and add two frames to it.

splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)

We can also add the splitter to another splitter widget.

QSplitter widget

Figure: QSplitter widget

List (QComboBox)

QComboBox Is a widget that allows users to choose from a list of options.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import (QWidget, QLabel, 
    QComboBox, QApplication)
import sys

class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):      

        self.lbl = QLabel("Ubuntu", self)

        combo = QComboBox(self)
        combo.addItem("Ubuntu")
        combo.addItem("Mandriva")
        combo.addItem("Fedora")
        combo.addItem("Arch")
        combo.addItem("Gentoo")

        combo.move(50, 50)
        self.lbl.move(50, 150)

        combo.activated[str].connect(self.onActivated)        
         
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QComboBox')
        self.show()
        
        
    def onActivated(self, text):
      
        self.lbl.setText(text)
        self.lbl.adjustSize()  
        
                
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

This example shows a  QComboBoxand a  QLabel. The combo box has a list of five options. These are the names of Linux distributions. The label widget displays the selected option in the combo box.

combo = QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Arch")
combo.addItem("Gentoo")

We QComboBoxcreate a widget with five options.

combo.activated[str].connect(self.onActivated) 

After selecting the item, we call the onActivated()method.

def onActivated(self, text):
  
    self.lbl.setText(text)
    self.lbl.adjustSize() 

Inside the method, we set the text of the selected item as the label widget. We adjust the size of the label.

QComboBox

Figure: QComboBox

Published 59 original articles · 69 praises · 270,000+ views

Guess you like

Origin blog.csdn.net/pansaky/article/details/98873529