PyQt5 – 访问标签的大小
我们可以用resize()方法来设置标签的大小,也可以用adjustSize()方法来调整与内容有关的大小。在这篇文章中,我们将看到我们如何访问尺寸。为了做到这一点,我们将使用size()方法。这个方法返回Qsize对象,它是resize()方法的参数。
语法: label.size()
参数: 它不需要参数。
返回: 它返回QtCore.Qsize对象。
代码。
# 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 geometry
self.setGeometry(100, 100, 600, 400)
# creating a label widget
self.label_1 = QLabel("old label ", self)
# setting up the border
self.label_1.setStyleSheet("border :3px solid black;")
# resizing the label
self.label_1.resize(150, 40)
# moving the label
self.label_1.move(100, 100)
# printing the size of old label
print(self.label_1.size())
# creating a new label widget
self.label_2 = QLabel("new Label ", self)
# setting up the border
self.label_2.setStyleSheet("border :3px solid black;")
# moving the label
self.label_2.move(200, 200)
# resizing the label
self.label_2.adjustSize()
# printing the size of new label
print(self.label_2.size())
# show all the widgets
self.update()
self.show()
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出:
PyQt5.QtCore.QSize(150, 40)
PyQt5.QtCore.QSize(150, 40)