def test_embed_widgets(qtbot): from PyQt5.QtWidgets import QVBoxLayout, QWidget window = QWidget() layout = QVBoxLayout(window) layout.addWidget(EntryWidget()) layout.addWidget(EntryWidget()) window.setLayout(layout) show({'qtbot': qtbot, 'widget': window})
def test_setFixedOption(qtbot): widget = EntryWidget() show(locals()) widget.setOptionFixed(True) assert widget.comboBox.isEnabled() is False assert widget.optionFixed() is True widget = EntryWidget() show(locals()) widget.setOptionFixed(False) assert widget.comboBox.isEnabled() is True assert widget.optionFixed() is False
def test_setEnabled(qtbot): widget = EntryWidget() show(locals()) widget.setEnabled(False) assert widget.lineEdit.isEnabled() is False assert widget.isReadOnly() is False assert widget.comboBox.isEnabled() is False assert widget.optionFixed() is True widget.setEnabled(True) assert widget.lineEdit.isEnabled() is True assert widget.isReadOnly() is False assert widget.comboBox.isEnabled() is True assert widget.optionFixed() is False
def test_errorCheck_live(qtbot): widget = EntryWidget(errorCheck=check_error_typed) show(locals()) logging.debug('typing...') qtbot.keyClicks(widget.lineEdit, 'erro') assert widget.getError() is False qtbot.keyClicks(widget.lineEdit, 'r') logging.debug('finished typing...') assert widget.getError() == 'ERROR' assert getCurrentColor( widget.lineEdit, 'Window').names[0] == widget.defaultColors['error'][0] qtbot.keyPress(widget.lineEdit, QtCore.Qt.Key_Backspace) assert widget.getError() is False assert getCurrentColor( widget.lineEdit, 'Window').names[0] == widget.defaultColors['default'][0]
def test_setError(qtbot): widget = EntryWidget() show(locals()) widget.setError(True) assert widget.getError() is True assert getCurrentColor( widget.lineEdit, 'Window').names[0] == widget.defaultColors['error'][0] widget.setError(False) assert widget.getError() is False assert getCurrentColor( widget.lineEdit, 'Window').names[0] == widget.defaultColors['blank'][0]
def test_constructor_readOnly(qtbot): widget = EntryWidget(readOnly=True) show(locals()) assert widget.lineEdit.isReadOnly() is True assert widget.isReadOnly() is True assert widget.comboBox.isEnabled() is False assert widget.optionFixed() is True widget = EntryWidget(readOnly=False) show(locals()) assert widget.lineEdit.isReadOnly() is False assert widget.isReadOnly() is False assert widget.comboBox.isEnabled() is True assert widget.optionFixed() is False
def test_onOptionChanged(qtbot): widget = EntryWidget(options=test_options_colors) show(locals()) widget.optionChanged.connect(lambda: change_color_on_option(widget)) widget.setSelected(test_options_colors[1]) assert getCurrentColor(widget.lineEdit, 'Window')[0][0] == test_options_colors[1] widget.setSelected(test_options_colors[0]) assert getCurrentColor(widget.lineEdit, 'Window')[0][0] == test_options_colors[0] widget.setSelected(test_options_colors[2]) assert getCurrentColor(widget.lineEdit, 'Window')[0][0] == test_options_colors[2]
def test_constructor_optionFixed(qtbot): widget = EntryWidget(optionFixed=True) show(locals()) assert widget.comboBox.isEnabled() is False assert widget.optionFixed() is True widget = EntryWidget(optionFixed=False) show(locals()) assert widget.comboBox.isEnabled() is True assert widget.optionFixed() is False
def test_errorCheck_with_combobox(qtbot): widget = EntryWidget(errorCheck=check_text_matches_option, options=['a', 'b']) show(locals()) qtbot.keyClicks(widget.lineEdit, 'b') assert widget.getError() is False qtbot.keyPress(widget.lineEdit, QtCore.Qt.Key_Backspace) qtbot.keyPress(widget.lineEdit, 'a') assert widget.getError() is True assert getCurrentColor( widget.lineEdit, 'Window').names[0] == widget.defaultColors['error'][0] widget.setSelected('b') assert widget.getError() is False qtbot.keyPress(widget.lineEdit, QtCore.Qt.Key_Backspace) qtbot.keyPress(widget.lineEdit, 'b') assert widget.getError() is True assert getCurrentColor( widget.lineEdit, 'Window').names[0] == widget.defaultColors['error'][0]
from PyQt5.QtWidgets import QWidget, QVBoxLayout from entrywidget import AutoColorLineEdit, EntryWidget # Qt stuff # from PyQt5.Qt import QApplication # optional, can be started from widget.mkQApp() # from PyQt5.QtWidgets import QApplication # app = QApplication(sys.argv) # start Qt app = EntryWidget.mkQApp() # <editor-fold desc="Support Funcs"> def check_error_typed(widget): """Returns 'ERROR' if widget.text() == 'error', False otherwise""" print('check_error_typed: ', widget.text()) if widget.text() == 'error': return 'ERROR' return False def check_text_matches_option(widget): """Compares widget.text() and widget.getSelected(), returns if they are ==""" v = widget.text() == widget.getSelected() print('check_text_matches_option:', v) return v def printer(label): """Creates a print function that prefixes the output with `label`""" return lambda *args: print(label, *args) # </editor-fold>
def test_constructor_options(qtbot): widget = EntryWidget(options=test_options_good) show(locals()) assert widget.getOptions() == {k: k for k in test_options_good} assert widget.getSelected() == test_options_good[0]
window = QWidget() # QLineEdit that changes color automatically autocolor = AutoColorLineEdit(window, text='AutoColorLineEdit', errorCheck=check_error_typed) autocolor.hasError[object].connect(printer('hasError[object] !!!')) # autocolor.hasError[str].connect(printer('hasError[str] !!!')) # autocolor.hasError.connect(printer('hasError !!!')) autocolor.errorCleared.connect(lambda: print(':) NO MORE ERROR :)')) # AutoColorLineEdit and a DictComboBox entry = EntryWidget(window, text='EntryWidget', options={ 'a': 'data A', 'b': 'data B', 'c': 'data C' }, errorCheck=check_text_matches_option) entry.optionChanged[str].connect(printer('optionChanged[str]')) # entry.optionChanged.connect(printer('optionChanged')) # entry.optionIndexChanged[int].connect(printer('optionIndexChanged[int]')) # entry.optionIndexChanged.connect(printer('optionIndexChanged')) entry.dataChanged[object].connect(printer('dataChanged[object]')) # entry.dataChanged[str].connect(printer('dataChanged[str]')) # entry.dataChanged.connect(printer('dataChanged')) # put a vertical layout in the window layout = QVBoxLayout(window) # add widgets to layout
def test_hasError(qtbot): widget = EntryWidget() widget.hasError.connect(lambda: set_title_on_error(widget)) show(locals()) assert widget.getError() is False widget.setError(False) assert widget.getError() is False widget.setError(True) assert widget.windowTitle() == 'ERROR' widget.clearError() assert widget.getError() is None
def test_setOptions(qtbot): widget = EntryWidget() show(locals()) widget.setOptions(test_options_good) assert widget.getOptions() == {k: k for k in test_options_good} assert widget.getSelected() == test_options_good[0]
from PyQt5.QtWidgets import QWidget, QVBoxLayout from PyQt5.Qt import QApplication from entrywidget import AutoColorLineEdit, EntryWidget app = QApplication([]) window = QWidget() window.setWindowTitle('EntryWidget examples') layout = QVBoxLayout(window) autocolor = AutoColorLineEdit(window, text='AutoColorLineEdit') layout.addWidget(autocolor) layout.addWidget(AutoColorLineEdit(text='')) errorwidget = AutoColorLineEdit(text='error', errorCheck=lambda w: (w.text() == 'error')) layout.addWidget(errorwidget) entry = EntryWidget(window, text='EntryWidget', options=['QComboBox']) layout.addWidget(entry) window.setLayout(layout) window.show() app.exec_()
def test_constructor_basic(qtbot): widget = EntryWidget() show(locals())
# logging stuff import logging logging.basicConfig(stream=sys.stdout, level=logging.INFO) # <editor-fold desc="Support Funcs"> def change_color_on_option(entry_widget): print('change_color to', entry_widget.getSelected()) entry_widget.setColors((entry_widget.getSelected(), 'black')) # </editor-fold> print("\n----------------------- Standard Usage") widget = EntryWidget(options=['opt1', 'opt2', 'opt3'], text='Prompt Text') widget.setWindowTitle('Standard usage') widget.show() app.exec_() print("\n----------------------- Printing text") widget = EntryWidget(text='type here') widget.textChanged[str].connect(print) widget.setWindowTitle('Print text') widget.show() app.exec_() print("\n----------------------- Select a Color") widget = EntryWidget(options=['orange', 'red', 'blue', 'white'], text='pick a color') widget.optionChanged.connect(lambda: change_color_on_option(widget))
def __init__(self, parent=None, **kwargs): self._value = None EntryWidget.__init__(self, parent, **kwargs) # runs setupUi