簡體   English   中英

PyQt5:標簽在網格布局中從頂部和底部截斷

[英]PyQt5: Labels cut off top and bottom in grid layout

我正在嘗試通過 for 循環向對話框 window 添加標簽。 理想情況下,我希望行高可以根據內容進行調整,這樣如果字符串是一行,則行高是一行,而如果字符串是 5 行,行會反映這一點。 這樣我可以通過所有內容向 go 添加滾動條。

相反,我在每個 label 的頂部和底部得到了這個奇怪的截斷:

from PyQt5 import QtWidgets

app = QtWidgets.QApplication([])

window = QtWidgets.QDialog()
grid = QtWidgets.QGridLayout()
window.setLayout(grid)

window.setFixedWidth(200)
window.setFixedHeight(600)

for label in range(5):
    label = QtWidgets.QLabel()
    label.setText(2 * "This is a label with slightly too much text for this little window. Therefore it would be great to wrap this text and have the row size be adjusted automatically.")
    label.setWordWrap(True)
    grid.addWidget(label)

    label2 = QtWidgets.QLabel()
    label2.setText(2 * "HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA")
    label2.setWordWrap(True)
    grid.addWidget(label2)

window.show()
app.exec_()

我運行程序時的結果

關於關於固定寬度/高度的注釋,這是注釋掉固定寬度和高度的結果:

聽起來你想要一個 QScrollArea:

from PyQt5 import QtWidgets

app = QtWidgets.QApplication([])

window = QtWidgets.QScrollArea(widgetResizable=True)
widget = QtWidgets.QWidget()
window.setWidget(widget)
grid = QtWidgets.QGridLayout(widget)

window.setFixedWidth(200)
window.setFixedHeight(600)

for label in range(5):
    label = QtWidgets.QLabel()
    label.setText(2 * "This is a label with slightly too much text for this little window. Therefore it would be great to wrap this text and have the row size be adjusted automatically.")
    label.setWordWrap(True)
    grid.addWidget(label)

    label2 = QtWidgets.QLabel()
    label2.setText(2 * "HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA HABALABOBA")
    label2.setWordWrap(True)
    grid.addWidget(label2)

window.show()
app.exec_()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM