QLabel widget In PyQt5
PyQt5 : QLabel widget .
QLabel widget is used for adding some Text or images on our PyQt5 main window .
Label is used to instruct or present some data to user .
Label is the basic and common widget of any GUI Framework .
.
In this Article you will learn how to create a Label using QLabel and also work with its methods .
So lets code :
# PyQt5 : Dynamic Coding # adding Label . from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel from PyQt5 import QtGui import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Dynamic Coding') self.setGeometry(100,100,300,200) self.setFixedSize(300,200) self.setWindowIcon(QtGui.QIcon('icon.png')) label = QLabel("Hello world",self) if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
Lets Understand The code :
first we import QLabel from Qtwidgets class and and in the constructor section we create a QLabel of name label and call QLabel with two arguments :
1. String of label.
2. where to place it .
Output :
As you can see the above code create adds a label on main window but the problem is the label is not aligned prefectly it is at position 0,0 .
So now lets change the position of our label .
Changing position of label :
# PyQt5 : Dynamic Coding # resize change label position. from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel from PyQt5 import QtGui import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Dynamic Coding') self.setGeometry(100,100,300,200) self.setFixedSize(300,200) self.setWindowIcon(QtGui.QIcon('icon.png')) label = QLabel(self) label.setText('Dynamic Coding') label.move(120,80) if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
for changing the position of label or any widget you can use move method of PyQt5 .
move method takes two argument :
1. position from x axis .
2 .position from y axis .
I also use a method setText( ) which is used to change the text of Qlabel or any GUI widget of PyQt5 .
Output :
# PyQt5 : Dynamic Coding # change label font from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel from PyQt5 import QtGui import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Dynamic Coding') self.setGeometry(100,100,300,200) self.setWindowIcon(QtGui.QIcon('icon.png')) label = QLabel(self) label.setText('Dynamic Coding') label.setFixedWidth(200) # or use resize(120,50)
label.setFont(QtGui.QFont('Times',12,QtGui.QFont.Bold)) label.move(80,80) if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
For changing the font size , family and style we use a QtGui method named QFont which takes 3 arguments .
- font family
- font size
- font style
instead of using setFixedWidth you can also use resize method which changes the width as well as height of label or setFixedsize method .
Output :
So what is styleSheet , stylesheet is a methods of PyQt5 which takes agruments like CSS ,
( CSS is a language for describing the look and formatting of content. Usually this content is in the form of HTML however it is possible to be in other formats as well. CSS is organised as a set of rules that define how different parts of the content should be presented. )
In our code we are using a type of CSS which is called as inline CSS .
In pyqt5 we can used the same attributes and values that are used in CSS .
# PyQt5 : Dynamic Coding # change label font using stylesheeet from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel from PyQt5 import QtGui import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Dynamic Coding') self.setGeometry(100,100,300,200) self.setWindowIcon(QtGui.QIcon('icon.png')) label = QLabel(self) label.setText('Dynamic Coding') label.setFixedWidth(200) label.setStyleSheet("font-size:18px;font-family:Times New Roman;") label.move(80,80) if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
Output :
Changing color of label using stylesheet :
# PyQt5 : Dynamic Coding # change label font using stylesheeet from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel from PyQt5 import QtGui import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Dynamic Coding') self.setGeometry(100,100,300,200) self.setWindowIcon(QtGui.QIcon('icon.png')) label = QLabel(self) label.setText('Dynamic Coding') label.setFixedWidth(160) label.setStyleSheet("font-size:18px;font-family:Times New Roman;color:red;background-color:black") label.move(80,80) if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
Output :
Adding image at label :
# PyQt5 : Dynamic Coding # image at label from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel from PyQt5 import QtGui import sys class Example(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Dynamic Coding') self.setGeometry(100,100,300,200) self.setWindowIcon(QtGui.QIcon('icon.png')) label = QLabel('Sublime text',self) label.setPixmap(QtGui.QPixmap("sublime.jpg")) label.resize(label.sizeHint()) label.move(80,80) if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_())
for adding image at Label we use a method setPixmap and in it we pass the image name with its extension under QtGui.QPixmap which converts the image into PyQt5 useable fromat .
Output :
.
.
Check out our Previous PyQt5 Posts .
.
# -----------------------------------------------------------------------------------------------------------------------------------------------------#
No comments