Python : 屏幕输入/输出

目录

# 屏幕输入/输出

从「工具 – 宏 - 运行宏...」菜单运行 Python 宏时，Python 标准输出文件不可用。展示模块的输出内容需要 Python 交互式控制台。Python shell 提供 input()、print()、repr()、str() 等功能。

LibreOffice msgbox Python module proposes a msgbox() method that is illustrated in Creating Event Listeners and Creating a dialog handler example pages.

LibreOffice Basic 提供 InputBox()、Msgbox() 与 Print() 屏幕输入/输出函数。现有的 Python 替代品有的依赖 LibreOffice API 抽象窗口工具集 (Abstract Windowing Toolkit)，有的依赖 Python 向 Basic 的函数调用。后者提供有意接近 Basic 的语法，同时使用 Python 模块与 Basic 模块。API 脚本框架被用于执行 Basic、BeanShell、JavaScript 以及 Python 的跨语言函数调用。

## Python 语法:

MsgBox(txt, buttons=0, title=None)

InputBox(txt, title=None, default=None)

Print(txt)

## 示例:

>>> import screen_io as ui

>>> reply = ui.InputBox('Please enter a phrase', title='Dear user', defaultValue="here..")

>>> rc = ui.MsgBox(reply, title="Confirmation of phrase")

>>> age = ui.InputBox('How old are you?', title="Hi")

>>> ui.Print(age)

## 安装:

- 将 Python 模块「screen_io」复制到「我的宏」，位于 <UserProfile>/Scripts/python/pythonpath,

- 将 Basic 模块「uiScripts」复制到「我的宏」标准 Basic 库,

- 重新启动 LibreOffice。

### screen_io Python 模块

            # -*- coding: utf-8 -*-
            from __future__ import unicode_literals

            def MsgBox(prompt: str, buttons=0, title='LibreOffice') -> int:
                """ 显示含有消息的对话框，并返回一个值。"""
                xScript = _getScript("_MsgBox")
                res = xScript.invoke((prompt,buttons,title), (), ())
                return res[0]

            def InputBox(prompt: str, title='LibreOffice', defaultValue='') -> str:
                """ 在对话框中显示提示，用户可以在对话框内输入文字。"""
                xScript = _getScript("_InputBox")
                res = xScript.invoke((prompt,title,defaultValue), (), ())
                return res[0]

            def Print(message: str):
                """在对话框中输出指定字符串或数字表达式。"""
                xScript = _getScript("_Print")
                xScript.invoke((message,), (), ())

            import uno
            from com.sun.star.script.provider import XScript
            def _getScript(script: str, library='Standard', module='uiScripts') -> XScript:
                sm = uno.getComponentContext().ServiceManager
                mspf = sm.createInstanceWithContext("com.sun.star.script.provider.MasterScriptProviderFactory", uno.getComponentContext())
                scriptPro = mspf.createScriptProvider("")
                scriptName = "vnd.sun.star.script:"+library+"."+module+"."+script+"?language=Basic&location=application"
                xScript = scriptPro.getScript(scriptName)
                return xScript

MsgBox and InputBox methods from the Basic service included in the ScriptForge libraries call directly their native Basic counterparts.

### uiScripts Basic 模块

            Option Explicit
            Private Function _MsgBox( prompt As String, Optional buttons As Integer, _
                    Optional title As String ) As Integer
                _MsgBox = MsgBox( prompt, buttons, title )
            End Function
            Private Function _InputBox( prompt As String, Optional title As String, _
                    Optional default As String) As String
                _InputBox = InputBox( prompt, title, default )
            End Function
            Private Sub _Print( msg As String )
                Print msg
            End Sub

The Alternative Python Script Organizer (APSO) extension offers a msgbox() function out of its apso_utils module.

相关主题

屏幕 I/O 函数

Python 编程示例

LibreOffice Python 脚本帮助

从 Python 中调用 BASIC 宏