Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. how do i separate these sudoku boxes (grids)
Forum Updated to NodeBB v4.3 + New Features

how do i separate these sudoku boxes (grids)

Scheduled Pinned Locked Moved Unsolved Language Bindings
4 Posts 2 Posters 931 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nullbuil7
    wrote on last edited by nullbuil7
    #1

    hi i have this code and i don't know how to add spacing between boxes of sudoku and how to add some sort of separator line. can you help me?

    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, 800, 800)
            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()
    
            gridLayout.addLayout(box1,0,0)
            gridLayout.addLayout(box2,0,1)
            gridLayout.addLayout(box3,0,2)
            gridLayout.addLayout(box4,1,0)
            gridLayout.addLayout(box5,1,1)
            gridLayout.addLayout(box6,1,2)
            gridLayout.addLayout(box7,2,0)
            gridLayout.addLayout(box8,2,1)
            gridLayout.addLayout(box9,2,2)
    
            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])
    
    1 Reply Last reply
    0
    • N Offline
      N Offline
      nullbuil7
      wrote on last edited by
      #2

      ok so i finally managed to make a separator using QFrame (turns out i was missing a parenthesis) . but it's showing buggy behavior. i don't know why this is happening :

      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, 800, 800)
              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()
      
              line = QFrame()
              line.setFrameShape(QFrame.HLine)
              line.setFrameShadow(QFrame.Sunken)
      
              gridLayout.addLayout(box1,0,0)
              gridLayout.addLayout(box2,0,1)
              gridLayout.addLayout(box3,0,2)
              gridLayout.addWidget(line, 1, 0)
              gridLayout.addWidget(line, 1, 1)
              gridLayout.addWidget(line, 1, 2)
              gridLayout.addLayout(box4,2,0)
              gridLayout.addLayout(box5,2,1)
              gridLayout.addLayout(box6,2,2)
              gridLayout.addLayout(box7,3,0)
              gridLayout.addLayout(box8,3,1)
              gridLayout.addLayout(box9,3,2)
      
              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])
      
      1 Reply Last reply
      0
      • N Offline
        N Offline
        nullbuil7
        wrote on last edited by
        #3

        after tinkering with the problem I'm 100% certain this is a issue with qt (maybe just pyside2) the code is correct but the QFrame line keep disappearing first i thought it was the QFrame approach so i created a line with Paint and used those still the same issue lines keep disappearing. need a completely different approach to this problem:

        import sys
        from PySide2.QtWidgets import *
        from PySide2.QtGui 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, 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)
        
        
        
        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])
        
        1 Reply Last reply
        0
        • jazzycamelJ Offline
          jazzycamelJ Offline
          jazzycamel
          wrote on last edited by jazzycamel
          #4

          @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 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, 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)
          

          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)

          For the avoidance of doubt:

          1. All my code samples (C++ or Python) are tested before posting
          2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved