PDA

View Full Version : how do i fix this weird bug?



mohsentux
15th December 2019, 17:25
hi new here (also this code is PySide2) . I'm trying to create a sudoku grid but my QFrame lines don't show up ( when i change the size of window some of them show up!!). i don't know how to fix this. i use the QFrame to create a separator but it doesn't work as intended.

import sys
from PySide2.QtWidgets import *

class SudokuGrid(QWidget):
def __init__(self):
super(SudokuGrid, self).__init__()
self.initGUI()

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, 3)
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, 3)
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"), pos[i][0], pos[i][1])
box2.addWidget(QLabel("1"), pos[i][0], pos[i][1])
box3.addWidget(QLabel("1"), pos[i][0], pos[i][1])
box4.addWidget(QLabel("1"), pos[i][0], pos[i][1])
box5.addWidget(QLabel("1"), pos[i][0], pos[i][1])
box6.addWidget(QLabel("1"), pos[i][0], pos[i][1])
box7.addWidget(QLabel("1"), pos[i][0], pos[i][1])
box8.addWidget(QLabel("1"), pos[i][0], pos[i][1])
box9.addWidget(QLabel("1"), pos[i][0], pos[i][1])
i = i + 1


self.setLayout(gridLayout)



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])

d_stranz
16th December 2019, 16:54
You cannot add the same widget instance to more than one position in a layout. By repeatedly adding the same QFrame instances (hline, vline) to different positions in the gridLayout, all you are doing is moving the two instances around, not adding a copy.

But IMO, your design is a disaster. You should seriously consider using the Qt Graphics / View architecture as the basis for your UI. You would implement your Sudoku grid as a custom QAbstractGraphicsShapeItem or maybe derive from QGraphicsRectItem with a custom paint() method to draw the interior 3x3 grids. The squares themselves could be QGraphicsSimpleTextItem instances that are children of the grid item. You would set the QGraphicsItem::ItemIsSelectable and QGraphicsItem::ItemIsFocusable flags to allow the user to click on and type into them.