We continue to introduce PyQt5 controls. This time there are QPixmap
, QLineEdit
, QSplitter
, and QComboBox
.
picture
QPixmap
It is a component that processes images. In this example, we use to QPixmap
display an image in the window.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
In this example, we dispay an image
on the window.
Author: Jan Bodnar
Website: zetcode.com
Last edited: August 2017
"""
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_())
pixmap = QPixmap("redrock.png")
Create an QPixmap
object that receives a file as an argument.
lbl = QLabel(self)
lbl.setPixmap(pixmap)
Put QPixmap
the instance QLabel
in the component.
Program display:
row edit
QLineEdit
The component provides the function of editing text, with functions such as undo, redo, cut, paste, drag and drop.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
This example shows text which
is entered in a QLineEdit
in a QLabel widget.
Author: Jan Bodnar
Website: zetcode.com
Last edited: August 2017
"""
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_())
The example shows an editing component and a label. The text we type in the input box will be displayed in the label immediately.
qle = QLineEdit(self)
Create an QLineEdit
object.
qle.textChanged[str].connect(self.onChanged)
If the value of the input box changes, onChanged()
the method is called.
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
Inside onChanged()
the method, we assign the value in the text box to the label component, and then call adjustSize()
the method to make the label adapt to the text content.
Program display:
QSplitter
QSplitter
The component allows the user to change the size of the child window by dragging the dividing line. QFrame
In this example we show three components separated by two dividing lines .
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
This example shows
how to use QSplitter widget.
Author: Jan Bodnar
Website: zetcode.com
Last edited: August 2017
"""
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()
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
The layout of three windows and two dividing lines has been created, but it should be noted that under some themes, the display effect of dividing lines is not very good.
topleft = QFrame(self)
topleft.setFrameShape(QFrame.StyledPanel)
In order to see the dividing line more clearly, we use the set sub-window style.
splitter1 = QSplitter(Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)
Create a QSplitter
component and add two frames inside.
splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)
It can also be divided inside the dividing line.
Program display:
drop down box
QComboBox
A component that allows the user to select one of several options.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
This example shows how to use
a QComboBox widget.
Author: Jan Bodnar
Website: zetcode.com
Last edited: August 2017
"""
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 contains one QComboBox
and one QLabel
. There are five options in the drop-down selection box, all of which are Linux distribution names, and the label content is the selected distribution name.
combo = QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Arch")
combo.addItem("Gentoo")
Create a QComboBox
component and five options.
combo.activated[str].connect(self.onActivated)
Call the method on the selected item onActivated()
.
def onActivated(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
Inside the method, set the label content to the selected string, then set the adaptive text size.