Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. Bob_widget = widget['Bob'] # lookup widget by objectName
  2. assert Bob_widget.layout.coordinates == (0, 0)
  3.  
  4. import sys
  5. from collections import namedtuple
  6.  
  7. from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QGridLayout,
  8. QLabel, QDoubleSpinBox)
  9.  
  10. gui_userinput = namedtuple('gui_userinput', ['key', 'string', 'value', 'range'])
  11.  
  12. variable_a = gui_userinput(key='variable_a', string='Variable A', value=165.00,
  13. range=(0.00, 10000.00))
  14. variable_b = gui_userinput(key='variable_b', string='Variable B', value=135.00,
  15. range=(0.00, 10000.00))
  16.  
  17. gui_userinput_order = (variable_a, variable_b)
  18.  
  19.  
  20. class Gui(QMainWindow):
  21. def __init__(self, parent=None):
  22.  
  23. super(Gui, self).__init__(parent)
  24.  
  25. self.setObjectName('toplevel')
  26. self.setupUi()
  27.  
  28. def setupUi(self):
  29.  
  30. centralWidget = QWidget()
  31. centralWidget.setObjectName('centralwidget')
  32. self.setCentralWidget(centralWidget)
  33.  
  34. centralLayout = QGridLayout()
  35. self.setObjectName('centrallayout')
  36.  
  37. for i, widget in enumerate(gui_userinput_order):
  38.  
  39. wlabel = QLabel(widget.string)
  40. wlabel.setObjectName(widget.key + '_label')
  41.  
  42. wbox = QDoubleSpinBox()
  43. wbox.setObjectName(widget.key)
  44. wbox.setRange(*widget.range)
  45. wbox.setValue(widget.value)
  46.  
  47. centralLayout.addWidget(wlabel, 0, i)
  48. centralLayout.addWidget(wbox, 1, i)
  49.  
  50. centralWidget.setLayout(centralLayout)
  51.  
  52.  
  53. if __name__ == '__main__':
  54.  
  55. app = QApplication(sys.argv)
  56. ui = Gui()
  57. ui.show()
  58. sys.exit(app.exec_())
  59.  
  60. widget_0_0 = self.ui.centralWidget().layout()
  61. .itemAtPosition(0, 0).widget()
  62.  
  63. import sys
  64. from PyQt5.QtWidgets import QApplication, QDoubleSpinBox, QLabel
  65.  
  66. import mini_ui
  67.  
  68. app = QApplication(sys.argv)
  69.  
  70.  
  71. class TestMainGUi:
  72.  
  73. def setup(self):
  74. self.ui = mini_ui.Gui()
  75.  
  76. def test_all_widgets_exist(self):
  77. """The user finds the expected labels and doublespinboxes."""
  78. for widget in mini_ui.gui_userinput_order:
  79. found_box = self.ui.findChild(QDoubleSpinBox, widget.key)
  80. assert found_box.value() == widget.value
  81. found_label = self.ui.findChild(QLabel, widget.key + '_label')
  82. assert found_label.text() == widget.string
  83.  
  84. def test_label_0_0(self):
  85. """The user sees a label for 'Variable A' in the top left corner."""
  86. label_a = self.ui.findChild(QLabel, 'variable_a_label')
  87. print('Found', label_a, 'named', label_a.objectName(), 'in GUI.')
  88. widget_0_0 = self.ui.centralWidget().layout()
  89. .itemAtPosition(0, 0).widget()
  90. print('Found widget type', type(widget_0_0), 'at grid 0, 0')
  91. print('It is named: ', widget_0_0.objectName())
  92.  
  93. assert widget_0_0.text() == 'Variable A'
  94. assert widget_0_0 is label_a
  95.  
  96. def test_alternate_widget_acces(self):
  97. """Just hacking away trying to find a more terse widget reference"""
  98. label_a = self.ui.findChild(QLabel, 'variable_a_label')
  99.  
  100. assert 1 == 1
  101.  
  102. def teardown(self):
  103. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement