class EmbeddedTerminal(Box): '''Embedded Terminal class.''' def __init__(self, parent_widget, *args, **kwargs): Box.__init__(self, parent_widget, *args, **kwargs) self.output = Entry(self, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.output.editable_set(False) self.output.scrollable_set(True) self.output.callback_changed_add(self.cb_changed) self.output.show() frame = Frame(self, size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ) frame.text = 'Input:' frame.autocollapse_set(True) frame.collapse_go(True) frame.show() hbx = Box(self, size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ) hbx.horizontal = True hbx.show() frame.content = hbx self.input = Entry(self, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.input.single_line_set(True) self.input.callback_activated_add(self.cb_enter) self.input.show() enter_btn = Button(self) enter_btn.text = 'Execute' enter_btn.callback_pressed_add(self.cb_enter) enter_btn.show() hbx.pack_end(self.input) hbx.pack_end(enter_btn) self.pack_end(self.output) self.pack_end(frame) self.cmd_exe = None self.done_cb = None # pylint: disable=no-self-use def cb_changed(self, obj): '''Output changed, move cursor''' obj.cursor_end_set() def cb_enter(self, btn): '''Enter pressed on input''' if not self.cmd_exe: self.run_cmd(self.input.text) self.input.text = '' else: self.cmd_exe.send(f'{self.input.text}\n') self.input.text = '' def run_cmd(self, command, done_cb=None): '''Run command capture ouput''' command = markup_to_utf8(command) # pylint: disable=c-extension-no-member self.cmd_exe = cmd = ecore.Exe( command, ecore.ECORE_EXE_PIPE_READ | ecore.ECORE_EXE_PIPE_ERROR | ecore.ECORE_EXE_PIPE_WRITE) cmd.on_add_event_add(self.cb_started) cmd.on_data_event_add(self.cb_data) cmd.on_error_event_add(self.cb_error) cmd.on_del_event_add(self.cb_done) self.done_cb = done_cb def cb_started(self, cmd, event, *args, **kwargs): '''Command start''' self.output.entry_append('---------------------------------') self.output.entry_append('<br>') def cb_data(self, cmd, event, *args, **kwargs): '''Stdout callback''' self.output.entry_append(f'{event.data}') self.output.entry_append('<br>') def cb_error(self, cmd, event, *args, **kwargs): '''Stderr callback''' self.output.entry_append(f'Error: {event.data}') def cb_done(self, cmd, event, *args, **kwargs): '''Command finished callback''' self.output.entry_append('---------------------------------') self.output.entry_append('<br>') self.cmd_exe = None if self.done_cb: if callable(self.done_cb): self.done_cb()
class CommandOutputEntry(Table): def __init__(self, parent, min_size=(0,0)): Table.__init__(self, parent, size_hint_expand=EXPAND_BOTH, size_hint_fill=FILL_BOTH) self._entry = Entry(self, scrollable=True, editable=False, line_wrap=ELM_WRAP_NONE, size_hint_expand=EXPAND_BOTH, size_hint_fill=FILL_BOTH) self._wheel = Progressbar(self, style='wheel', pulse_mode=True, size_hint_expand=EXPAND_BOTH) self._rect = Rectangle(self.evas, size_hint_min=min_size, size_hint_expand=EXPAND_BOTH, color=(0,0,0,0)) self.pack(self._entry, 0, 0, 1, 1) self.pack(self._rect, 0, 0, 1, 1) self.pack(self._wheel, 0, 0, 1, 1) self._last_was_carriage = False self._entry.show() self._rect.show() self.show() @property def text(self): return self._entry.text @text.setter def text(self, text): self._entry.text = text def pulse_start(self): self._rect.repeat_events = False self._wheel.pulse(True) self._wheel.show() def pulse_stop(self): self._rect.repeat_events = True self._wheel.pulse(False) self._wheel.hide() def successfull(self): self._entry.entry_append('<success>Operation successfully completed.</success><br>') def failure(self): self._entry.entry_append('<failure>Error! Something goes wrong.</failure><br>') def error_set(self, text): self._entry.text = '<failure>Error:</failure><br>%s' % text def append_raw(self, line, sep=None): if self._last_was_carriage is True: self._entry.cursor_selection_begin() self._entry.cursor_line_end_set() self._entry.cursor_selection_end() self._entry.entry_insert('') if sep == '\n': self._entry.entry_append(line + '<br>') self._entry.cursor_end_set() self._last_was_carriage = False elif sep == '\r': self._entry.entry_append(line) self._last_was_carriage = True else: self._entry.entry_append(line) self._last_was_carriage = False
class EmbeddedTerminal(Box): def __init__(self, parent_widget, titles=None, *args, **kwargs): Box.__init__(self, parent_widget, *args, **kwargs) self.outPut = Entry(self, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.outPut.editable_set(False) self.outPut.scrollable_set(True) self.outPut.callback_changed_add(self.changedCb) self.outPut.show() frame = Frame(self, size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ) frame.text = "Input:" frame.autocollapse_set(True) frame.collapse_go(True) frame.show() bx = Box(self, size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ) bx.horizontal = True bx.show() frame.content = bx self.inPut = Entry(self, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.inPut.single_line_set(True) self.inPut.callback_activated_add(self.enterPressed) self.inPut.show() enterButton = Button(self) enterButton.text = "Execute" enterButton.callback_pressed_add(self.enterPressed) enterButton.show() bx.pack_end(self.inPut) bx.pack_end(enterButton) self.pack_end(self.outPut) self.pack_end(frame) self.cmd_exe = None self.done_cb = None def changedCb(self, obj): obj.cursor_end_set() def enterPressed(self, btn): if not self.cmd_exe: self.runCommand(self.inPut.text) self.inPut.text = "" else: ourResult = self.cmd_exe.send("%s\n" % self.inPut.text) self.inPut.text = "" def runCommand(self, command, done_cb=None): self.cmd_exe = cmd = ecore.Exe( command, ecore.ECORE_EXE_PIPE_READ | ecore.ECORE_EXE_PIPE_ERROR | ecore.ECORE_EXE_PIPE_WRITE) cmd.on_add_event_add(self.command_started) cmd.on_data_event_add(self.received_data) cmd.on_error_event_add(self.received_error) cmd.on_del_event_add(self.command_done) self.done_cb = done_cb def command_started(self, cmd, event, *args, **kwargs): self.outPut.entry_append("---------------------------------") self.outPut.entry_append("<br>") def received_data(self, cmd, event, *args, **kwargs): self.outPut.entry_append("%s" % event.data) self.outPut.entry_append("<br>") def received_error(self, cmd, event, *args, **kwargs): self.outPut.entry_append("Error: %s" % event.data) def command_done(self, cmd, event, *args, **kwargs): self.outPut.entry_append("---------------------------------") self.outPut.entry_append("<br>") self.cmd_exe = None if self.done_cb: if callable(self.done_cb): self.done_cb()
class EmbeddedTerminal(Box): def __init__(self, parent_widget, titles=None, *args, **kwargs): Box.__init__(self, parent_widget, *args, **kwargs) self.outPut = Entry(self, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.outPut.editable_set(False) self.outPut.scrollable_set(True) self.outPut.callback_changed_add(self.changedCb) self.outPut.show() frame = Frame(self, size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ) frame.text = "Input:" frame.autocollapse_set(True) frame.collapse_go(True) frame.show() bx = Box(self, size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ) bx.horizontal = True bx.show() frame.content = bx self.inPut = Entry(self, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.inPut.single_line_set(True) self.inPut.callback_activated_add(self.enterPressed) self.inPut.show() enterButton = Button(self) enterButton.text = "Execute" enterButton.callback_pressed_add(self.enterPressed) enterButton.show() bx.pack_end(self.inPut) bx.pack_end(enterButton) self.pack_end(self.outPut) self.pack_end(frame) self.cmd_exe = None self.done_cb = None def changedCb(self, obj): obj.cursor_end_set() def enterPressed(self, btn): if not self.cmd_exe: self.runCommand(self.inPut.text) self.inPut.text = "" else: ourResult = self.cmd_exe.send("%s\n" % self.inPut.text) self.inPut.text = "" def runCommand(self, command, done_cb=None): self.cmd_exe = cmd = ecore.Exe( command, ecore.ECORE_EXE_PIPE_READ | ecore.ECORE_EXE_PIPE_ERROR | ecore.ECORE_EXE_PIPE_WRITE ) cmd.on_add_event_add(self.command_started) cmd.on_data_event_add(self.received_data) cmd.on_error_event_add(self.received_error) cmd.on_del_event_add(self.command_done) self.done_cb = done_cb def command_started(self, cmd, event, *args, **kwargs): self.outPut.entry_append("---------------------------------") self.outPut.entry_append("<br>") def received_data(self, cmd, event, *args, **kwargs): self.outPut.entry_append("%s" % event.data) self.outPut.entry_append("<br>") def received_error(self, cmd, event, *args, **kwargs): self.outPut.entry_append("Error: %s" % event.data) def command_done(self, cmd, event, *args, **kwargs): self.outPut.entry_append("---------------------------------") self.outPut.entry_append("<br>") self.cmd_exe = None if self.done_cb: if callable(self.done_cb): self.done_cb()
class Calculator(object): def __init__(self): """Creates the window and its contents. :attribute memory: The first operand. :attribute operand: The operand to join the both values. :attribute field: The input/display field. :attribute table: Formatting table holding the buttons. """ # Create mathematical attributes. self.memory = '' self.operand = None self.display_is_result = False # Create the main window. self.window = StandardWindow("eCalculate", "eCalculate", autodel=True, size=(300, 300)) # Create box that holds all GUI elements. box = Box(self.window, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) self.window.resize_object_add(box) box.show() # Create Input field. self.field = Entry(self.window, single_line=True, size_hint_weight=EXPAND_HORIZONTAL, size_hint_align=FILL_HORIZONTAL) self.field.markup_filter_append(self.filter_markup) box.pack_end(self.field) self.field.show() # Create table holding the buttons. self.table = Table(self.window, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) box.pack_end(self.table) self.table.show() # Create buttons. self.add_button(x=0, y=2, text='1', char='1') self.add_button(x=1, y=2, text='2', char='2') self.add_button(x=2, y=2, text='3', char='3') self.add_button(x=0, y=1, text='4', char='4') self.add_button(x=1, y=1, text='5', char='5') self.add_button(x=2, y=1, text='6', char='6') self.add_button(x=0, y=0, text='7', char='7') self.add_button(x=1, y=0, text='8', char='8') self.add_button(x=2, y=0, text='9', char='9') self.add_button(x=0, y=3, text='0', char='0') self.add_button(x=4, y=0, text='÷', char='/') self.add_button(x=4, y=1, text='×', char='*') self.add_button(x=4, y=2, text='−', char='-') self.add_button(x=4, y=3, text='+', char='+') self.add_button(x=2, y=3, text='=', char='=') self.add_button(x=1, y=3, text='.', char='.') # Finally show the window. self.window.show() def add_button(self, x, y, text, char): """Add a button to the user interface. :param x: The horizontal position in the UI-table. :param y: The vertical position in the UI-table. :param text: The button label. :param action: String what the button should do, resolved into a function in self.do_action(). :param char: Character that is passed along with the action. """ button = Button(self.window, text=text, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) button.callback_clicked_add(self.enter_char, char=char) self.table.pack(button, x, y, 1, 1) button.show() def enter_char(self, caller, char): """Adds a character to the input field. Called if a button is pressed. :param caller: The clicked button. :param char: The character that should be added to the field. """ self.field.entry_append(char) def calculate(self): """Do the calculation. This function does not check if the values are valid. """ result = self.CALC_ACTIONS[self.operand](float(self.memory), float(self.field.text)) self.field.text = re.sub('\.0$', '', str(result)) self.memory = '' def filter_markup(self, caller, char, _): """Filter the entry field of not allowed characters, check that only one point is in the value, do calculations and set the operand. Called whenever text is inserted either throught the keyboard or by pressing buttons. :param caller: The calling object (always self.field). :param char: The character entered. :param _: I do not know ;-) (Passed automatically, always None?). """ if not ((not char in '1234567890.+-*/=') or \ (char == '.' and ('.' in caller.text or caller.text == ''))): if char == '=' and self.memory and caller.text: self.calculate() self.display_is_result = True elif char in '1234567890.': if not self.display_is_result: return char else: self.display_is_result = False if char == '=': self.memory = '' caller.text = '' elif char in self.CALC_ACTIONS: if self.memory and caller.text: self.calculate() self.operand = char if caller.text: self.memory = caller.text caller.text = '' CALC_ACTIONS = { '+': lambda a, b: a + b, '-': lambda a, b: a - b, '*': lambda a, b: a * b, '/': lambda a, b: a / b, }