PyQt5 – 如何创建标签的虚线边框?
在本文中,我们将了解如何在 PyQt5 应用程序中创建标签的虚线边框。一般来说,当我们创建一个带边框的标签时,边框是连续的,但我们也可以制作一个虚线边框。下面显示了正常和虚线边框的不同之处。
为了做到这一点,我们将使用setStyleSheet()
方法。
Syntax : label.setStyleSheet(“border :3px black; border-style : dashed”)
Argument : It takes string as argument
Action performed : It makes the border dashed.
代码 :
# 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("Normal border", self)
# moving position
self.label_1.move(100, 100)
# setting up the border and padding
self.label_1.setStyleSheet("border :3px solid black;")
# resizing label
self.label_1.resize(100, 80)
# creating a label widget
self.label_2 = QLabel("Dashed border", self)
# setting up the dashed border
self.label_2.setStyleSheet("border :3px solid black;
border-style : dashed")
# moving position
self.label_2.move(230, 200)
# resizing the label
self.label_2.resize(100, 80)
# 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())
输出 :