Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Bob_widget = widget['Bob'] # lookup widget by objectName
- assert Bob_widget.layout.coordinates == (0, 0)
- import sys
- from collections import namedtuple
- from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QGridLayout,
- QLabel, QDoubleSpinBox)
- gui_userinput = namedtuple('gui_userinput', ['key', 'string', 'value', 'range'])
- variable_a = gui_userinput(key='variable_a', string='Variable A', value=165.00,
- range=(0.00, 10000.00))
- variable_b = gui_userinput(key='variable_b', string='Variable B', value=135.00,
- range=(0.00, 10000.00))
- gui_userinput_order = (variable_a, variable_b)
- class Gui(QMainWindow):
- def __init__(self, parent=None):
- super(Gui, self).__init__(parent)
- self.setObjectName('toplevel')
- self.setupUi()
- def setupUi(self):
- centralWidget = QWidget()
- centralWidget.setObjectName('centralwidget')
- self.setCentralWidget(centralWidget)
- centralLayout = QGridLayout()
- self.setObjectName('centrallayout')
- for i, widget in enumerate(gui_userinput_order):
- wlabel = QLabel(widget.string)
- wlabel.setObjectName(widget.key + '_label')
- wbox = QDoubleSpinBox()
- wbox.setObjectName(widget.key)
- wbox.setRange(*widget.range)
- wbox.setValue(widget.value)
- centralLayout.addWidget(wlabel, 0, i)
- centralLayout.addWidget(wbox, 1, i)
- centralWidget.setLayout(centralLayout)
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- ui = Gui()
- ui.show()
- sys.exit(app.exec_())
- widget_0_0 = self.ui.centralWidget().layout()
- .itemAtPosition(0, 0).widget()
- import sys
- from PyQt5.QtWidgets import QApplication, QDoubleSpinBox, QLabel
- import mini_ui
- app = QApplication(sys.argv)
- class TestMainGUi:
- def setup(self):
- self.ui = mini_ui.Gui()
- def test_all_widgets_exist(self):
- """The user finds the expected labels and doublespinboxes."""
- for widget in mini_ui.gui_userinput_order:
- found_box = self.ui.findChild(QDoubleSpinBox, widget.key)
- assert found_box.value() == widget.value
- found_label = self.ui.findChild(QLabel, widget.key + '_label')
- assert found_label.text() == widget.string
- def test_label_0_0(self):
- """The user sees a label for 'Variable A' in the top left corner."""
- label_a = self.ui.findChild(QLabel, 'variable_a_label')
- print('Found', label_a, 'named', label_a.objectName(), 'in GUI.')
- widget_0_0 = self.ui.centralWidget().layout()
- .itemAtPosition(0, 0).widget()
- print('Found widget type', type(widget_0_0), 'at grid 0, 0')
- print('It is named: ', widget_0_0.objectName())
- assert widget_0_0.text() == 'Variable A'
- assert widget_0_0 is label_a
- def test_alternate_widget_acces(self):
- """Just hacking away trying to find a more terse widget reference"""
- label_a = self.ui.findChild(QLabel, 'variable_a_label')
- assert 1 == 1
- def teardown(self):
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement