Created
January 11, 2014 12:37
-
-
Save sanfx/8370512 to your computer and use it in GitHub Desktop.
generate controls in a modal dialog prompt. The data is passed in the form of python dictionary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment