Can you tell me how to delete spaces between buttons im my Widget? I want to do it by seting command , setSpacing(0)
, but I don't know where I should set it.
My code:
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_())
Ok, I got it. If someone will have this problem here is a solution. I changed my conception, class Saper inherents from QWidget and now table names contains smaller tabels with names. First for counts elements in big table and second for counts elements in smaller tabels. I'm adding each element to one horizontal box and then I'm adding this horizontal box to vertical box, I repeat this action for each row. Important thing is to make new horizontal box after adding old one to vertical box:
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_())