首页 文章

调试OOo UNO-Python

提问于
浏览
1

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

com.sun.star.uno.RuntimeException错误在模块文件中调用函数main时:/// C:/path/to/test.py(:'module'对象没有属性'MessageBox'C:\ path \ to \函数main()中的test.py:34 [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 回答

  • 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")
    

相关问题