当前位置: 首页>>代码示例>>Python>>正文


Python JTextArea.addKeyListener方法代码示例

本文整理汇总了Python中javax.swing.JTextArea.addKeyListener方法的典型用法代码示例。如果您正苦于以下问题:Python JTextArea.addKeyListener方法的具体用法?Python JTextArea.addKeyListener怎么用?Python JTextArea.addKeyListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.swing.JTextArea的用法示例。


在下文中一共展示了JTextArea.addKeyListener方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: PythonWindow

# 需要导入模块: from javax.swing import JTextArea [as 别名]
# 或者: from javax.swing.JTextArea import addKeyListener [as 别名]
class PythonWindow(KeyListener):
    def __init__(self):
        self.frame = JFrame("Python Window")
        self.historyList = JList(DefaultListModel())
        self.historyList.cellRenderer = MyListCellRenderer()
        #self.historyPanel.layout = BoxLayout(
        #    self.historyPanel,
        #    BoxLayout.Y_AXIS
        #)
        scrollpane = JScrollPane()
        #    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
        #    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
        #)
        # scrollpane.preferredSize = 400, 800
        inputPanel = JPanel()
        inputPanel.layout = GridLayout(1, 1)
        self.input = JTextArea("")
        self.input.border = BorderFactory.createEmptyBorder(5, 5, 5, 5)
        self.input.tabSize = 4
        self.input.font = Font("Monospaced", Font.PLAIN, 12)
        #self.input.preferredSize = 500, 200 
        self.input.addKeyListener(self)
        #self.button = JButton('Run', actionPerformed=self.run)
        inputPanel.add(self.input)
        #inputPanel.add(self.button)
        scrollpane.viewport.view = self.historyList
        self.frame.add(scrollpane, BorderLayout.CENTER)
        self.frame.add(inputPanel, BorderLayout.PAGE_END)
        self.frame.size = 500, 600
        self.frame.visible = False
    def toggle_visibility(self):
        self.frame.visible = not self.frame.visible
    def add(self, text, type="input"):
        self.historyList.model.addElement({"text": text, "type": type})
        self.historyList.validate()
        self.frame.validate()
        last = self.historyList.model.getSize() - 1
        self.historyList.ensureIndexIsVisible(last)
    def write(self, text):
        self.add(text, "output")
    def run(self, evt):
        source = self.input.text
        if not source.strip():
            self.input.text = ""
            return
        processed_source = source.replace("$", "geo.")
        code = interface.compileinteractive(processed_source)
        if code in ("continue", "error"):
            code = interface.compilemodule(processed_source)
            if code == "error":
                return
        self.add(source.strip())
        result = interface.run(code)
        if result == "OK":
            self.input.text = ""
    def keyPressed(self, evt):
        pass
    def keyReleased(self, evt):
        pass
    def keyTyped(self, evt):
        if evt.keyChar == '\n':
            # Only try to run compound statements when they end with
            # two \n
            source = self.input.text
            lines = source.split("\n")
            if lines[0].rstrip().endswith(":") and not source.endswith("\n\n"):
                for i, c in enumerate(lines[-2]):
                    if c not in ' \t': break
                else:
                    self.run(evt)
                    return
                prefix = lines[-2][:i]
                if lines[-2].endswith(":"):
                    prefix += '\t'
                self.input.text = source + prefix
            else:
                self.run(evt)
开发者ID:,项目名称:,代码行数:79,代码来源:

示例2: PythonWindow

# 需要导入模块: from javax.swing import JTextArea [as 别名]
# 或者: from javax.swing.JTextArea import addKeyListener [as 别名]
class PythonWindow(KeyListener):
    def __init__(self):
        self.frame = JFrame("Python Window")
        #self.historyList = JList(DefaultListModel())
        #self.historyList.cellRenderer = MyListCellRenderer()
        scrollpane = JScrollPane()
        inputPanel = JPanel()
        inputPanel.layout = GridLayout(1, 1)
        self.input = JTextArea("")
        self.input.border = BorderFactory.createEmptyBorder(5, 5, 5, 5)
        self.input.tabSize = 4
        self.input.font = Font("Monospaced", Font.PLAIN, 12)
        self.input.addKeyListener(self)
        inputPanel.add(self.input)
        self.outputpane = OutputPane()
        scrollpane.viewport.view = self.outputpane.textpane  #self.historyList
        self.frame.add(scrollpane, BorderLayout.CENTER)
        self.frame.add(inputPanel, BorderLayout.PAGE_END)
        self.frame.size = 500, 600
        self.frame.visible = False
        self.component = None

    def toggle_visibility(self):
        self.frame.visible = not self.frame.visible

    def add_component(self, c):
        self.remove_component()
        self.frame.add(c, BorderLayout.PAGE_START)
        self.component = c

    def remove_component(self):
        if self.component is not None:
            self.frame.remove(self.component)
            self.component = None

    def add(self, text, type="input"):
        self.outputpane.addtext(text, type)
        #self.historyList.model.addElement({"text": text, "type": type})
        #self.historyList.validate()
        self.frame.validate()
        #last = self.historyList.model.getSize() - 1
        #self.historyList.ensureIndexIsVisible(last)
    def error(self, text):
        self.outputpane.addtext(text, "error", ensure_newline=True)

    def write(self, text):
        self.add(text, "output")

    def run(self, evt):
        source = self.input.text
        if not source.strip():
            self.input.text = ""
            return
        processed_source = source.replace("$", "geo.")
        code = interface.compileinteractive(processed_source)
        if code in ("continue", "error"):
            code = interface.compilemodule(processed_source)
            if code == "error":
                return
        source = source.strip() + '\n'
        self.outputpane.addtext(source, "input", ensure_newline=True)
        result = interface.run(code)
        if result == "OK":
            self.input.text = ""

    def keyPressed(self, evt):
        pass

    def keyReleased(self, evt):
        pass

    def keyTyped(self, evt):
        if evt.keyChar == '\n':
            # Only try to run compound statements when they end with
            # two \n
            source = self.input.text
            lines = source.split("\n")
            if lines[0].rstrip().endswith(":") and not source.endswith("\n\n"):
                for i, c in enumerate(lines[-2]):
                    if c not in ' \t': break
                else:
                    self.run(evt)
                    return
                prefix = lines[-2][:i]
                if lines[-2].endswith(":"):
                    prefix += '\t'
                self.input.text = source + prefix
            else:
                self.run(evt)
开发者ID:avilleret,项目名称:geogebra,代码行数:91,代码来源:pyggb.py


注:本文中的javax.swing.JTextArea.addKeyListener方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。