PyQt:QGridLayout中的setSpacing

时间:2017-07-06 08:36:36

标签: python user-interface python-3.5 pyqt5 qgridlayout

你能告诉我如何删除我的Widget按钮之间的空格吗?我想通过设置命令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_())

1 个答案:

答案 0 :(得分:0)

好的,我明白了。如果有人在这里有这个问题是一个解决方案。我从QWidget改变了我的概念,类Saper inherents,现在表名包含更小的名称表。首先计算大表中的元素,然后计算较小表中的计数元素。我将每个元素添加到一个水平框中,然后我将此水平框添加到垂直框中,我对每一行重复此操作。重要的是在将旧的水平框添加到垂直框后创建新的水平框:

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_())