combobox.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (c) <2015-Present> Tzutalin
  2. # Copyright (C) 2013 MIT, Computer Science and Artificial Intelligence Laboratory. Bryan Russell, Antonio Torralba,
  3. # William T. Freeman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. # associated documentation files (the "Software"), to deal in the Software without restriction, including without
  5. # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  6. # Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  8. # the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  9. # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  10. # SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  12. # THE SOFTWARE.
  13. import sys
  14. try:
  15. from PyQt5.QtWidgets import QWidget, QHBoxLayout, QComboBox
  16. except ImportError:
  17. # needed for py3+qt4
  18. # Ref:
  19. # http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html
  20. # http://stackoverflow.com/questions/21217399/pyqt4-qtcore-qvariant-object-instead-of-a-string
  21. if sys.version_info.major >= 3:
  22. import sip
  23. sip.setapi('QVariant', 2)
  24. from PyQt4.QtGui import QWidget, QHBoxLayout, QComboBox
  25. class ComboBox(QWidget):
  26. def __init__(self, parent=None, items=[]):
  27. super(ComboBox, self).__init__(parent)
  28. layout = QHBoxLayout()
  29. self.cb = QComboBox()
  30. self.items = items
  31. self.cb.addItems(self.items)
  32. self.cb.currentIndexChanged.connect(parent.comboSelectionChanged)
  33. layout.addWidget(self.cb)
  34. self.setLayout(layout)
  35. def update_items(self, items):
  36. self.items = items
  37. self.cb.clear()
  38. self.cb.addItems(self.items)