lundi 22 juillet 2019

Using PyQt5, how can you "word wrap" a list of labelled checkboxes?

I have a number of labelled checkboxes that I want to display sequentially, in a "word wrap" fashion. But each time I add a checkbox it just makes the program wider. Any ideas on how one might achieve a word wrap effect?

from PyQt5.QtWidgets import (
  QDialog,
  QApplication,
  QGroupBox,
  QHBoxLayout,
  QVBoxLayout
)
import sys
from PyQt5 import QtWidgets


class Window(QDialog):
  def __init__(self):
    super().__init__()

    self.top = 200
    self.left = 200
    self.width = 200
    self.height = 400
    self.InitWindow()

  def InitWindow(self):
    self.setGeometry(self.left,  self.top, self.width, self.height)

    self.createLayout()
    vbox = QVBoxLayout()
    vbox.addWidget(self.groupBox)
    self.setLayout(vbox)
    self.show()

  def createLayout(self):
    self.groupBox = QGroupBox("Checkboxes:")
    hboxlayout = QHBoxLayout()

    self.A   = QtWidgets.QCheckBox("one")
    self.B   = QtWidgets.QCheckBox("two")
    self.C   = QtWidgets.QCheckBox("three")
    self.D   = QtWidgets.QCheckBox("four")
    self.E   = QtWidgets.QCheckBox("five")
    self.F   = QtWidgets.QCheckBox("six")
    self.G   = QtWidgets.QCheckBox("seven")
    self.H   = QtWidgets.QCheckBox("eight")
    self.checkboxes = [self.A, self.B, self.C, self.D, self.E, self.F, self.G, self.H]

    for checkbox in self.checkboxes:
        hboxlayout.addWidget(checkbox)
    hboxlayout.addStretch()

    self.groupBox.setLayout(hboxlayout)

if __name__ == "__main__":
  App = QApplication(sys.argv)
  window = Window()
  sys.exit(App.exec())




Aucun commentaire:

Enregistrer un commentaire