我正在尝试在 PyQt5 中重新创建这个 。 问题是两个标签(标题和副标题)之间的空格。
这是我当前的代码:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt
class NoResultsWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# "No results found" -> heading
self.noResultsLabel = QLabel("No results found", self)
self.noResultsLabel.setAlignment(Qt.AlignCenter)
self.noResultsLabel.setStyleSheet("font-size: 24px;")
# "Try different keywords or remove search filters" -> subheading
self.instructionLabel = QLabel(
"Try different keywords or remove search filters", self
)
self.instructionLabel.setAlignment(Qt.AlignCenter)
self.instructionLabel.setStyleSheet("font-size: 14px; color: gray;")
layout = QVBoxLayout()
layout.addWidget(self.noResultsLabel)
layout.addWidget(self.instructionLabel)
layout.setSpacing(10)
self.setLayout(layout)
self.setWindowTitle("No Results")
self.setGeometry(300, 300, 350, 200)
self.show()
def main():
app = QApplication(sys.argv)
ex = NoResultsWidget()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
有谁知道如何去掉两个标签之间的空格?或者想到另一种方法来做到这一点?
我认为你可以使用 label.setGeometry 函数将其放在你想要的位置。我不知道它也可以在 python 中工作,所以让我知道它是否有效! :) 在c++(也许是python)中,我们做 YourLabel.setGeometry(posX,posY,height,width)