I have attempted the GUI controls creation in a prompt based on data from dictionary. Is this the best way of implementing this?
import sys
from PyQt4 import QtCore, QtGui
class MessageBox(QtGui.QDialog):
"""docstring for MessageBox"""
def __init__(self, data=None, parent=None):
super(MessageBox, self).__init__(parent)
self._data = data
self.buildUi()
def buildUi(self):
self.gridLayout = QtGui.QGridLayout()
self.gridLayout.setSpacing(10)
for index, (key, values) in enumerate(self._data.iteritems()):
getLbl = QtGui.QLabel("Get", self)
label = QtGui.QLabel(key, self)
chkBox = QtGui.QCheckBox(self._data[key][0], self)
chkBox.setToolTip("Click here to get the book")
version = QtGui.QSpinBox( self)
version.setValue(self._data[key][-1])
version.setRange(self._data[key][-1], 12)
self.gridLayout.addWidget(getLbl, index, 0)
self.gridLayout.addWidget(label, index, 1)
self.gridLayout.addWidget(chkBox, index, 2)
self.gridLayout.addWidget(version, index, 3)
self.layout = QtGui.QVBoxLayout()
self.okBtn = QtGui.QPushButton("OK")
self.layout.addLayout(self.gridLayout)
self.horLayout = QtGui.QHBoxLayout()
self.horLayout.addStretch(1)
self.horLayout.addWidget(self.okBtn)
self.layout.addLayout(self.horLayout)
self.setLayout(self.layout)
class MainWindow(QtGui.QMainWindow):
"""docstring for MainWindow"""
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
# self.widget = FormWidget()
self._data = {
'Contact':['Carl Sagan', 2],
'End of Faith':['Sam Harris', 7],
'On Mars':['Patrick Moore', 1],
}
self.btn = QtGui.QPushButton("Hello", self)
self.btn.clicked.connect(self._launchMessageBox)
self.show()
def _launchMessageBox(self):
dlg = MessageBox(self._data)
dlg.exec_()
def main():
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
# window.raise_()
sys.exit(app.exec_())
if __name__ == '__main__':
main()