调试 OOo UNO-Python

Debugging OOo UNO-Python

我正在尝试在 LibreOffice Calc 中读取和解析 CSV 文件。我需要显示文本以调试我的逻辑,我发现的第一件事是 this。恼人的是,它重复了 OOo Basic 中内置的功能。第一个实现尝试使用一个不存在的函数;如果我直接调用它(使用“工具”菜单中的 TestMessageBox),第二个会起作用,但是当我从我的 pythonpath 目录中包含它时,我得到一个错误:

com.sun.star.uno.RuntimeExceptionError during invoking function main in module file:///C:/path/to/test.py (: 'module' object has no attribute 'MessageBox' C:\path\to\test.py:34 in function main() [msgbox.MessageBox(parentwin, message, 'Title')]
C:\Program Files (x86)\LibreOffice 5\program\pythonscript.py:870 in function invoke() [ret = self.func( *args )] )

为什么没有属性MessageBox

我是这样调用它的:

import msgbox

def main():
    doc = XSCRIPTCONTEXT.getDocument()
    parentwin = doc.CurrentController.Frame.ContainerWindow

    message = "Message"
    msgbox.MessageBox(parentwin, message, 'Title')

    return

这里是 pythonpath/msgbox.py:

import uno

from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY
from com.sun.star.awt.MessageBoxButtons import DEFAULT_BUTTON_OK, DEFAULT_BUTTON_CANCEL, DEFAULT_BUTTON_RETRY, DEFAULT_BUTTON_YES, DEFAULT_BUTTON_NO, DEFAULT_BUTTON_IGNORE

from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX

def TestMessageBox():
  doc = XSCRIPTCONTEXT.getDocument()
  parentwin = doc.CurrentController.Frame.ContainerWindow

  s = "This a message"
  t = "Title of the box"
  res = MessageBox(parentwin, s, t, QUERYBOX, BUTTONS_YES_NO_CANCEL + DEFAULT_BUTTON_NO)

  s = res
  MessageBox(parentwin, s, t, "infobox")

# Show a message box with the UNO based toolkit
def MessageBox(ParentWin, MsgText, MsgTitle, MsgType=MESSAGEBOX, MsgButtons=BUTTONS_OK):
  ctx = uno.getComponentContext()
  sm = ctx.ServiceManager
  sv = sm.createInstanceWithContext("com.sun.star.awt.Toolkit", ctx) 
  myBox = sv.createMessageBox(ParentWin, MsgType, MsgButtons, MsgTitle, MsgText)
  return myBox.execute()

g_exportedScripts = TestMessageBox,

软件包名称 msgbox 已在 UNO 中使用。参见 msgbox.MsgBox。为您的模块选择一个不同的名称,例如 mymsgbox.py。更好的是,将其移动到 pythonpath 内的包(子目录),例如 mystuff.msgbox.MessageBox.

其实我刚才试了msgbox.MsgBox,好像有用:

import msgbox

def main():
    message = "Message"
    myBox = msgbox.MsgBox(XSCRIPTCONTEXT.getComponentContext())
    myBox.addButton("oK")
    myBox.renderFromButtonSize()
    myBox.numberOflines = 2
    myBox.show(message,0,"Title")