Ejemplo n.º 1
0
    def init(self):
        if self.isInit:
            return

        # add screens
        self.screen = Screen(name=szConsole)
        self.editorLayout = EditorLayout(self)
        self.tutorialLayout = TutorialLayout(self)
        self.fileBrowser = FileBrowser(self)

        self.repl = REPL(self)
        self.history = [
            "",
        ]
        self.historyIndex = -1
        self.historyCount = 100
        self.lastIndentSpace = ""
        self.bIndentMode = False
        self.reFocusInputText = False
        self.oldTouchPrev = None
        self.textInputWidth = W * (4.0 / 5.0)

        # console menu layout
        self.consoleMenuLayout = BoxLayout(orientation="horizontal",
                                           size_hint=(1, None),
                                           height="35dp")
        btn_clear = Button(text="Clear", background_color=darkGray)
        btn_clear.bind(on_release=lambda inst: self.clearOutput())
        btn_prev = Button(text="<<", background_color=darkGray)
        btn_next = Button(text=">>", background_color=darkGray)
        self.consoleMenuLayout.add_widget(btn_clear)
        self.consoleMenuLayout.add_widget(btn_prev)
        self.consoleMenuLayout.add_widget(btn_next)
        self.screen.add_widget(self.consoleMenuLayout)

        # screen menu layout
        self.screenMenuLayout = BoxLayout(orientation="horizontal",
                                          size_hint=(1, None),
                                          height="35dp",
                                          pos=(0, 0))
        btn_console = Button(text="Console",
                             background_color=[1.5, 0.8, 0.8, 2])
        btn_editor = Button(text="Code Editor",
                            background_color=[0.8, 1.5, 0.8, 2])
        btn_tutorial = Button(text="Python Tutotial",
                              background_color=[0.8, 0.8, 1.5, 2])
        btn_editor.bind(on_release=lambda inst: self.setMode(szEditor))
        btn_tutorial.bind(on_release=lambda inst: self.setMode(szTutorial))
        self.screenMenuLayout.add_widget(btn_console)
        self.screenMenuLayout.add_widget(btn_editor)
        self.screenMenuLayout.add_widget(btn_tutorial)
        self.screen.add_widget(self.screenMenuLayout)

        # text input
        self.consoleInput = TextInput(text="text",
                                      multiline=False,
                                      size_hint=(None, None),
                                      auto_indent=True,
                                      font_name=defaultFont,
                                      background_color=(.1, .1, .1, 1),
                                      foreground_color=(1, 1, 1, 1),
                                      text_size=(0, 0),
                                      font_size="14dp",
                                      padding_x="20dp",
                                      padding_y="15dp")
        self.consoleInput.size = (W, self.consoleInput.minimum_height)
        self.consoleInput.text = ""
        '''
    def paste():
      self.consoleInput.insert_text(gMyRoot.getClipboard())
      self.refreshLayout()
    self.consoleInput.paste = paste
    '''
        self.consoleInput.bind(on_text_validate=self.onConsoleInput)
        self.consoleInput.bind(focus=self.inputBoxFocus)

        # textinput scroll view
        self.textInputSV = ScrollView(size_hint=(None, None),
                                      size=(W,
                                            self.consoleInput.minimum_height))
        self.textInputSV.scroll_y = 0
        self.textInputSV.add_widget(self.consoleInput)

        # add input widget
        self.screen.add_widget(self.textInputSV)

        # run button
        self.btn_run = Button(text="Run", size_hint=(None,None), size =(W-self.consoleInput.size[0], self.consoleInput.size[1]),\
          background_color=(1.3,1.3,2,2))
        self.btn_run.bind(on_release=lambda inst: self.onConsoleInput(
            self.consoleInput, True))
        self.btn_run.pos = (W - self.btn_run.size[0], 0)
        self.screen.add_widget(self.btn_run)

        # output
        self.outputSV = ScrollView(size_hint=(None, None))
        self.screen.add_widget(self.outputSV)

        self.outputLayout = BoxLayout(orientation="vertical",
                                      size_hint=(None, None))
        self.outputSV.add_widget(self.outputLayout)

        def func_prev(inst):
            if len(self.history) > 0:
                self.historyIndex -= 1
                if self.historyIndex < 0:
                    self.historyIndex = len(self.history) - 1
                text = self.history[self.historyIndex]
                if text.find("\n") > -1:
                    self.bIndentMode = True
                self.setInputText(text)

        btn_prev.bind(on_release=func_prev)

        def func_next(inst):
            if len(self.history) > 0:
                self.historyIndex += 1
                if self.historyIndex >= len(self.history):
                    self.historyIndex = 0
                text = self.history[self.historyIndex]
                if text.find("\n") > -1:
                    self.bIndentMode = True
                self.setInputText(text)

        btn_next.bind(on_release=func_next)

        # show output layout
        self.displayText("Python " + sys.version.strip(), 0)
        self.isInit = True