PyQt:QGridLayout 中的 setSpacing
PyQt: setSpacing in 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_())
好的,我明白了。如果有人会遇到这个问题,这里是一个解决方案。我改变了我的想法,class 来自 QWidget 的 Saper inherents,现在 table names 包含带有名称的更小的 tabels。首先用于大 table 中的计数元素,其次用于较小表格中的计数元素。我将每个元素添加到一个水平框,然后将这个水平框添加到垂直框,我对每一行重复此操作。重要的是在将旧的垂直框添加到垂直框后制作新的水平框:
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_())
你能告诉我如何删除我的 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_())
好的,我明白了。如果有人会遇到这个问题,这里是一个解决方案。我改变了我的想法,class 来自 QWidget 的 Saper inherents,现在 table names 包含带有名称的更小的 tabels。首先用于大 table 中的计数元素,其次用于较小表格中的计数元素。我将每个元素添加到一个水平框,然后将这个水平框添加到垂直框,我对每一行重复此操作。重要的是在将旧的垂直框添加到垂直框后制作新的水平框:
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_())