调试OOo UNO-Python

时间:2016-11-01 04:17:11

标签: python libreoffice libreoffice-calc uno

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

  调用函数main时的

com.sun.star.uno.RuntimeExceptionError   在模块中   文件:/// C:/path/to/test.py   (:'模块'对象没有属性   '的MessageBox' C:\路径\到\ test.py:34   在函数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,

1 个答案:

答案 0 :(得分:1)

包名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")