QMainwindow与具有gridlayout的centralwidget的间隔问题

时间:2018-05-02 13:02:32

标签: python python-3.x pyqt pyqt5

我编写了一个小小的Snake示例。

我创建了一个QMainWindowcentralWidget有一个gridlayout。 我似乎无法正确地调整间距。我以为我可以用setContentMargins()做到这一点,所以我把它放在任何地方我都可以,但空间仍然存在。我可能会监督一些事情。我认为这与我的网格有关。但我没有看到问题所在。想法?

这是一张图片: https://imgur.com/tHbf6tV

这是我的代码:

import sys
from PyQt5 import QtWidgets as qw
from PyQt5 import QtGui as qg
from PyQt5 import QtCore as qc

class Snake(qw.QMainWindow):

    def __init__(self, parent=None):
        super(Snake, self).__init__(parent)
        self.setContentsMargins(0,0,0,0)
        self.test()
        self.show()

    def test(self):
        self.centralwidget = qw.QWidget()
        self.setCentralWidget(self.centralwidget)
        self.centralwidget.setStyleSheet("background-color: green")

        self.gridLayout = qw.QGridLayout()
        self.gridLayout.setSpacing(0)
        # self.gridLayout.setContentsMargins(0, 0, 0, 0)

        self.vLayout = qw.QVBoxLayout(self.centralwidget)
        self.hLayout = qw.QHBoxLayout()

        self.hLayout.setContentsMargins(0, 0, 0, 0)
        self.vLayout.setContentsMargins(0, 0, 0, 0)

        # center the grid with stretch on both sides

        self.hLayout.addLayout(self.gridLayout)
        self.hLayout.addStretch(0)

        self.vLayout.addLayout(self.hLayout)
        # push grid to the top of the window
        self.vLayout.addStretch(0)

        self.buttons = []
        for i in range(24):
            l = []
            for j in range(24):
                b = qw.QPushButton()
                b.setFixedSize(20, 20)
                b.setStyleSheet("background-color: black")
                l.append(b)
                self.gridLayout.addWidget(b, i, j)
                self.gridLayout.setColumnMinimumWidth(j, 20)
            self.buttons.append(l)
            self.gridLayout.setRowMinimumHeight(i, 16)

        self.buttons[5][5].setStyleSheet("background-color: green")
        self.buttons[4][5].setStyleSheet("background-color: green")
        self.buttons[3][5].setStyleSheet("background-color: green")

if __name__ == '__main__':
    app = qw.QApplication(sys.argv)
    Menu = Snake()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

我想我已经解决了它。线索是在没有QHBoxLayout和QVBoxLayout的情况下重新创建我的网格。

这是我的代码:

import sys
from PyQt5 import QtWidgets as qw
from PyQt5 import QtGui as qg
from PyQt5 import QtCore as qc


class Snake(qw.QMainWindow):
    def __init__(self, parent=None):
        super(Snake, self).__init__(parent)
        self.setContentsMargins(0, 0, 0, 0)
        self.setFixedSize(20*24,20*24)
        self.test()
        self.show()


    def test(self):
        self.centralwidget = qw.QWidget()
        self.setCentralWidget(self.centralwidget)
        self.centralwidget.setStyleSheet("background-color: green")
        self.centralwidget.setContentsMargins(0, 0, 0, 0)

        self.gridLayout = qw.QGridLayout()
        self.gridLayout.setSpacing(0)
        self.gridLayout.setContentsMargins(0,0,0,0)


        self.buttons = []
        for i in range(24):
            for j in range(24):
                b = qw.QLabel("Knopf" + str(i) + "/" + str(j))
                b.setFixedSize(20, 20)
                b.setStyleSheet("background-color: black")
                self.gridLayout.addWidget(b, i, j)
                self.gridLayout.setColumnMinimumWidth(j, 20)
            self.gridLayout.setRowMinimumHeight(i, 16)

        self.centralwidget.setLayout(self.gridLayout)


if __name__ == '__main__':
    app = qw.QApplication(sys.argv)
    Menu = Snake()
    sys.exit(app.exec_())