@nullbuil7 said in how do i separate these sudoku boxes (grids):
import sys
from PySide2.QtWidgets import *
from PySide2.QtGui import *class SudokuGrid(QWidget):
def initGUI(self): self.setWindowTitle("Sudoku Grid Example") self.setGeometry(800, 800, 400, 350) self.setGrid() def setGrid(self): gridLayout = QGridLayout() box1 = QGridLayout() box2 = QGridLayout() box3 = QGridLayout() box4 = QGridLayout() box5 = QGridLayout() box6 = QGridLayout() box7 = QGridLayout() box8 = QGridLayout() box9 = QGridLayout() hline = QFrame() hline.setFrameShape(QFrame.HLine) hline.setFrameShadow(QFrame.Sunken) vline = QFrame() vline.setFrameShape(QFrame.VLine) vline.setFrameShadow(QFrame.Sunken) gridLayout.addLayout(box1,0,0) gridLayout.addWidget(vline, 0, 1) gridLayout.addLayout(box2,0,2) gridLayout.addWidget(vline, 0, 3) gridLayout.addLayout(box3,0,4) gridLayout.addWidget(hline, 1, 0, 1, 5) gridLayout.addLayout(box4,2,0) gridLayout.addWidget(vline, 2, 1) gridLayout.addLayout(box5,2,2) gridLayout.addWidget(vline, 2, 3) gridLayout.addLayout(box6,2,4) gridLayout.addWidget(hline, 3, 0, 1, 5) gridLayout.addLayout(box7,4,0) gridLayout.addWidget(vline, 4, 1) gridLayout.addLayout(box8,4,2) gridLayout.addWidget(vline, 4, 3) gridLayout.addLayout(box9,4,4) pos = [(0,0),(0,1),(0,2), (1,0),(1,1),(1,2), (2,0),(2,1),(2,2)] i = 0 while i < 9: box1.addWidget(QLabel("1", self), pos[i][0], pos[i][1]) box2.addWidget(QLabel("1", self), pos[i][0], pos[i][1]) box3.addWidget(QLabel("1", self), pos[i][0], pos[i][1]) box4.addWidget(QLabel("1", self), pos[i][0], pos[i][1]) box5.addWidget(QLabel("1", self), pos[i][0], pos[i][1]) box6.addWidget(QLabel("1", self), pos[i][0], pos[i][1]) box7.addWidget(QLabel("1", self), pos[i][0], pos[i][1]) box8.addWidget(QLabel("1", self), pos[i][0], pos[i][1]) box9.addWidget(QLabel("1", self), pos[i][0], pos[i][1]) i = i + 1 self.setLayout(gridLayout)
def init(self):
super(SudokuGrid, self).init()
self.initGUI()if name == 'main':
try:
myApp = QApplication(sys.argv)
myWindow = SudokuGrid()
myWindow.show()
myApp.exec_()
sys.exit(0)
except NameError:
print("Name Error:", sys.exc_info()[1])
except SystemExit:
print("Closing Window...")
except Exception:
print(sys.exc_info()[1])
Its not a bug in Qt or PySide2: you can't add a widget to a layout multiple times; you only create one each ofhline and vline and add them to multiple locations in your layout. See my example below where I create a new instance of hline and vline each time I use them (and tidied things up a bit):
from PySide2.QtWidgets import QWidget, QGridLayout, QFrame, QLabel class SudokuGrid(QWidget): def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.setWindowTitle("Sudoku Grid Example") self.setGeometry(800,800,400,350) l=QGridLayout(self) boxes=[] for i in range(9): col=i%3 row=i//3 box=QGridLayout() boxes.append(box) l.addLayout(box, row*2, col*2) if col<2: vline=QFrame() vline.setFrameShape(QFrame.VLine) vline.setFrameShadow(QFrame.Sunken) l.addWidget(vline, row*2, (col*2)+1) if col==2 and row<2: hline=QFrame() hline.setFrameShape(QFrame.HLine) hline.setFrameShadow(QFrame.Sunken) l.addWidget(hline, (row*2)+1, 0, 1, 5) for box in boxes: for i in range(9): col=i%3 row=i//3 box.addWidget(QLabel(str(i),self), row, col) if __name__=="__main__": from sys import argv, exit from PyQt5.QtWidgets import QApplication a=QApplication(argv) s=SudokuGrid() s.show() exit(a.exec_())I've tested this with PyQt5 not PySide2, but they are compatible and this code should work perfectly well.
Hope this helps ;o)