英文:
remove unnecessary blank space in Qlabel
问题
以下是您要翻译的代码部分:
import sys
from PyQt5.QtCore import Qt
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout
class Window2(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("About")
vbox = QVBoxLayout()
hboxes = list()
disclaimer = {
'Text': """
some text
""",
'Longer text': """
longer text longer text text longer text longer
"""
}
for label, text in disclaimer.items():
hbox = QHBoxLayout()
for t in (label, text):
l = QLabel(t)
l.setAlignment(Qt.AlignLeft)
l.setStyleSheet('border-style: solid; border-width: 1px; border-color: black;')
hbox.addWidget(l)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main_window = Window2()
sys.exit(app.exec_())
希望这可以帮助您理解和解决您的问题。
英文:
I want labels on the left to all have the same horizontal length while text aligned to the left.
Their vertical size equals vertical size of the respective right widget.
Labels on the right to take as little space as possible. Basically remove indents around text.
Something like below.
I have this code.
import sys
from PyQt5.QtCore import Qt
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout
class Window2(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("About")
vbox = QVBoxLayout()
hboxes = list()
disclaimer = {
'Text': """
some text
""",
'Longer text': """
longer text longer text text longer text longer
"""
}
for label, text in disclaimer.items():
hbox = QHBoxLayout()
for t in (label, text):
l = QLabel(t)
l.setAlignment(Qt.AlignLeft)
l.setStyleSheet('border-style: solid; border-width: 1px; border-color: black;')
hbox.addWidget(l)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main_window = Window2()
sys.exit(app.exec_())
I can't seem to figure out how it works/what is margin, indent, padding, spacing, stretch etc. Please help me understand and solve this problem.
答案1
得分: 3
有2个错误:
- 多行字符串添加了空格,因此您有两个选项:使用strip()将其删除或手动删除它们,在这种情况下,我将使用第二个选项。
- 不要在QVBoxLayout内使用嵌套的QHBoxLayout,因为它们不会保持对齐,而是使用QGridLayout。
class Window2(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("About")
disclaimer = {
"Text": "some text",
"Longer text": "longer text longer text text longer text longer",
}
gridlay = QGridLayout(self)
for i, (label, text) in enumerate(disclaimer.items()):
for j, t in enumerate((label, text)):
l = QLabel(t.strip())
l.setAlignment(Qt.AlignLeft)
l.setStyleSheet(
"border-style: solid; border-width: 1px; border-color: black;"
)
gridlay.addWidget(l, i, j)
self.show()
英文:
It has 2 errors:
- The multiline string adds spaces so you have 2 options: use strip() to remove them or remove them manually, in this case I will use the second option.
- Do not use nested QHBoxLayout inside a QVBoxLayout since they will not maintain alignment, instead use a QGridLayout.
class Window2(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("About")
disclaimer = {
"Text": """some text""",
"Longer text": """longer text longer text text longer text longer""",
}
gridlay = QGridLayout(self)
for i, (label, text) in enumerate(disclaimer.items()):
for j, t in enumerate((label, text)):
l = QLabel(t.strip())
l.setAlignment(Qt.AlignLeft)
l.setStyleSheet(
"border-style: solid; border-width: 1px; border-color: black;"
)
gridlay.addWidget(l, i, j)
self.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论