PySide6:如何删除 QHBoxLayout 中按钮之间的间距?
PySide6: How to remove spacing between buttons in QHBoxLayout?
如何在固定大小后删除按钮之间的间距?
每当我设置按钮的大小时,都会添加一个 space。
我尝试了 setSpacing
但它不起作用。这可以通过 sizePolicy
或其他方式完成吗?
这是我的代码:
from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setMinimumSize(QSize(800, 600))
self.setWindowTitle('Horizontal Layout')
self.container = QWidget()
self.horizontalLayout = QHBoxLayout()
self.button_1 = QPushButton('Button 1')
self.button_1.setFixedSize(QSize(70, 60))
self.button_2 = QPushButton('Button 2')
self.button_2.setFixedSize(QSize(70, 60))
self.button_3 = QPushButton('Button 3')
self.button_3.setFixedSize(QSize(70, 60))
self.horizontalLayout.addWidget(self.button_1)
self.horizontalLayout.addWidget(self.button_2)
self.horizontalLayout.addWidget(self.button_3)
self.container.setLayout(self.horizontalLayout)
self.setCentralWidget(self.container)
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
在按钮之后的 and/or 之前使用 addStretch()
。
您可以找到更多 here.
示例:
from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QApplication, QMainWindow,QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setMinimumSize(QSize(600, 400))
self.move(600, 300)
self.setWindowTitle('Horizontal Layout')
self.container = QWidget()
self.horizontalLayout = QHBoxLayout()
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setSpacing(0)
self.button_1 = QPushButton('Button 1')
self.button_1.setFixedSize(QSize(70, 60))
self.button_2 = QPushButton('Button 2')
self.button_2.setFixedSize(QSize(70, 60))
self.button_3 = QPushButton('Button 3')
self.button_3.setFixedSize(QSize(70, 60))
self.button_4 = QPushButton('Button 4')
self.button_4.setFixedSize(QSize(70, 60))
self.button_5 = QPushButton('Button 5')
self.button_5.setFixedSize(QSize(70, 60))
self.button_6 = QPushButton('Button 6')
self.button_6.setFixedSize(QSize(70, 60))
self.button_7 = QPushButton('Button 7')
self.button_7.setFixedSize(QSize(70, 60))
self.horizontalLayout.addWidget(self.button_1)
self.horizontalLayout.addWidget(self.button_2)
self.horizontalLayout.addWidget(self.button_3)
self.horizontalLayout.addStretch()
self.horizontalLayout.addWidget(self.button_4)
self.horizontalLayout.addWidget(self.button_5)
self.horizontalLayout.addStretch()
self.horizontalLayout.addWidget(self.button_6)
self.horizontalLayout.addWidget(self.button_7)
self.setLayout(self.horizontalLayout)
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
结果如下:
如何在固定大小后删除按钮之间的间距?
每当我设置按钮的大小时,都会添加一个 space。
我尝试了 setSpacing
但它不起作用。这可以通过 sizePolicy
或其他方式完成吗?
这是我的代码:
from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setMinimumSize(QSize(800, 600))
self.setWindowTitle('Horizontal Layout')
self.container = QWidget()
self.horizontalLayout = QHBoxLayout()
self.button_1 = QPushButton('Button 1')
self.button_1.setFixedSize(QSize(70, 60))
self.button_2 = QPushButton('Button 2')
self.button_2.setFixedSize(QSize(70, 60))
self.button_3 = QPushButton('Button 3')
self.button_3.setFixedSize(QSize(70, 60))
self.horizontalLayout.addWidget(self.button_1)
self.horizontalLayout.addWidget(self.button_2)
self.horizontalLayout.addWidget(self.button_3)
self.container.setLayout(self.horizontalLayout)
self.setCentralWidget(self.container)
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
在按钮之后的 and/or 之前使用 addStretch()
。
您可以找到更多 here.
示例:
from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QApplication, QMainWindow,QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setMinimumSize(QSize(600, 400))
self.move(600, 300)
self.setWindowTitle('Horizontal Layout')
self.container = QWidget()
self.horizontalLayout = QHBoxLayout()
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setSpacing(0)
self.button_1 = QPushButton('Button 1')
self.button_1.setFixedSize(QSize(70, 60))
self.button_2 = QPushButton('Button 2')
self.button_2.setFixedSize(QSize(70, 60))
self.button_3 = QPushButton('Button 3')
self.button_3.setFixedSize(QSize(70, 60))
self.button_4 = QPushButton('Button 4')
self.button_4.setFixedSize(QSize(70, 60))
self.button_5 = QPushButton('Button 5')
self.button_5.setFixedSize(QSize(70, 60))
self.button_6 = QPushButton('Button 6')
self.button_6.setFixedSize(QSize(70, 60))
self.button_7 = QPushButton('Button 7')
self.button_7.setFixedSize(QSize(70, 60))
self.horizontalLayout.addWidget(self.button_1)
self.horizontalLayout.addWidget(self.button_2)
self.horizontalLayout.addWidget(self.button_3)
self.horizontalLayout.addStretch()
self.horizontalLayout.addWidget(self.button_4)
self.horizontalLayout.addWidget(self.button_5)
self.horizontalLayout.addStretch()
self.horizontalLayout.addWidget(self.button_6)
self.horizontalLayout.addWidget(self.button_7)
self.setLayout(self.horizontalLayout)
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
结果如下: