Wednesday, July 3, 2024
HomeLanguagesPythonPyQt5 – Different border corner of Label

PyQt5 – Different border corner of Label

In this article, we will see how to make different types of curves for different corners. We can make borders to a label using setStyleSheet() method but default borders have right angles at the corners i.e not having any curves, but also we can use makes curves by setting border-radius. This will make same curve at all the four corner.

Below is the example of normal border vs different curves at different corners :

This can be done using setStyleSheet() :

Syntax :

label.setStyleSheet("border :3px solid blue;"
                    "border-top-left-radius :35px;"
                    "border-top-right-radius : 20px; "
                    "border-bottom-left-radius : 50px; "
                    "border-bottom-right-radius : 10px")

Argument : It takes string as argument.
Action performed : Changes the curve at each corner.

Code :




# importing the required libraries
  
from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 
import sys
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
  
        # set the title
        self.setWindowTitle("Python")
  
        # setting  the geometry of window
        self.setGeometry(60, 60, 600, 400)
  
  
        # creating a label widget
        self.label_1 = QLabel("Regular border", self)
  
        # moving position
        self.label_1.move(100, 100)
  
        # setting up the border
        self.label_1.setStyleSheet("border :3px solid blue;")
  
        # resizing label
        self.label_1.resize(100, 100)
        # creating a label widget
        self.label_2 = QLabel("Different curves", self)
  
        # moving position
        self.label_2.move(230, 200)
  
        # setting curves at each corner
        self.label_2.setStyleSheet("border :3px solid blue;"
                                   "border-top-left-radius :35px;"
                                   " border-top-right-radius : 20px; "
                                   "border-bottom-left-radius : 50px; "
                                   "border-bottom-right-radius : 10px")
  
        # resizing the label
        self.label_2.resize(100, 100)
  
        # show all the widgets
        self.show()
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())


Output :

pyqt-label-border

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments