PyQt:QGridLayout 中的 setSpacing

问题描述 投票:0回答:2

您能告诉我如何删除小部件中按钮之间的空格吗?我想通过设置命令来做到这一点,

setSpacing(0)
,但我不知道应该在哪里设置它。

我的代码:

import sys
from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import QMainWindow, QApplication, QGridLayout, 
QPushButton,QWidget


class Saper(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()  

    def initUI(self):   

        grid = QGridLayout() 
        self.setLayout(grid)    

        names = ['Cls', 'Bck', '', 'Close',
                 '7', '8', '9', '/',
                '4', '5', '6', '*',
                 '1', '2', '3', '-',
                '0', '.', '=', '+',
                'f','i','f','i']

        positions = [(i,j) for i in range(6) for j in range(4)] 

        for position, name in zip(positions, names): 

        if name == '':
            continue
        button = QPushButton(name)
        button.setMinimumSize(20,20)
        button.setMaximumSize(20,20)
        grid.addWidget(button, *position)


    self.setGeometry(300,300,300,300)
    self.setWindowTitle('Title')
    self.move(300, 150)
    self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Saper()
    sys.exit(app.exec_())
python user-interface python-3.5 pyqt5 qgridlayout
2个回答
0
投票

好的,我明白了。如果有人遇到这个问题,这里有一个解决方案。我改变了我的概念,类 Saper 本质上来自 QWidget,现在表名称包含带有名称的较小表格。第一个用于计算大表中的元素,第二个用于计算小表中的元素。我将每个元素添加到一个水平框,然后将此水平框添加到垂直框,对每一行重复此操作。重要的是在将旧的水平框添加到垂直框后创建新的水平框:

import sys
from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import (QWidget, QPushButton, 
QHBoxLayout, QVBoxLayout, QApplication)



class Saper(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()  

    def initUI(self):   

        vbox = QVBoxLayout()
        vbox.setSpacing(0)
        vbox.addStretch(1)


        names = [['Cls', 'Bck', 'Ok', 'Close'],
                 ['7', '8', '9', '/'],
                ['4', '5', '6', '*'],
                 ['1', '2', '3', '-'],
                ['0', '.', '=', '+'],
                ['f','i','f','i']]


        for pos in names:
            hbox = QHBoxLayout()
            hbox.addStretch(1)
            hbox.setSpacing(0)
            for name in pos:

                button = QPushButton(name)
                button.setMinimumSize(75,60)
                button.setMaximumSize(75,60)
                hbox.addWidget(button) 
            vbox.addLayout(hbox)

        self.setLayout(vbox)  
        self.setGeometry(300,300,300,300)
        self.setWindowTitle('Title')
        self.move(300, 150)
        self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Saper()
    sys.exit(app.exec_())

0
投票

为了使

.setSpacing()
.setContentMargins()
在 QGridLayout 中工作,我最后使用了以下代码。我不知道为什么,但效果很好。

GridLayout.addWidget(QLabel(''),MainLayout.rowCount(),0)
GridLayout.setRowStretch(MainLayout.rowCount(),1)
GridLayout.addWidget(QLabel(''),0,MainLayout.columnCount())
GridLayout.setColumnStretch(MainLayout.columnCount(),1)
© www.soinside.com 2019 - 2024. All rights reserved.