[Solved] Python Dialog example

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
brauart
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

[Solved] Python Dialog example

Post by brauart »

Hello

I started deveoping a small dialog for OO with Basic, but found it very limited, no structures, no simple sort for arrays ..... So then I thought about mixing Basic and Python, but then couldn not find a way to pass more then just strings to and from python.
So then I decided to do the entire dialog with python. But though I searched quite a lot I could not find a (small) example where a dialog's logic is implemented by python.
The logic should be able to get some data from the dialog and then as a result modify the dialog (e.g. just the value of a label) again.
Is anybody aware of such a thing?

best regards,
brauart
Last edited by robleyd on Wed Jul 29, 2020 1:55 pm, edited 2 times in total.
Reason: Add green tick
OpenOffice 4.1.7 on Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Python Dialog example

Post by JeJe »

You can create your own structures in Basic and you can find sorting algorithms easily too.

https://wiki.openoffice.org/wiki/Docume ... structions

https://wiki.openoffice.org/wiki/Sorting_and_searching

Basic has the IDE, plenty of documentation, and numerous threads to help you.

You're asking about something which in Basic is very simple. In Python you're stuck, resorting to a thread. and there's maybe a very very small number of people can help you. I'm not one, I just use Basic. It has all those advantages.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Python Dialog example

Post by Villeroy »

Code: Select all

def dialog_example(*args):
    ctx = XSCRIPTCONTEXT.getComponentContext()
    smgr = ctx.getServiceManager()
    dp = smgr.createInstanceWithContext("com.sun.star.awt.DialogProvider", ctx)
    dialog = dp.createDialog("vnd.sun.star.script:FreeHSQLDB.dlgFreeHSQLDB?location=application")
    dialog.execute()
    dialog.dispose()
opens the dialog which is installed with my viewtopic.php?f=21&t=77543
vnd.sun.star.script: is the protocol of an URL
FreeHSQLDB is the dialog library
dlgFreeHSQLDB is the dialog name
location=application refers to "My Macros" or "LibreOffice Macros"
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
brauart
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Python Dialog example

Post by brauart »

@JeJe
thank you for your post.

Having to implement a sort algorith myself (even copying it), having no associative arrays, no regular expressions ... does not sound like a great programming environment to me.
But maybe I have to go fight with Basic at the end as it seems simply impossibe to build and operate a dialog with pyhton. Meanwhile I found more people fighting with the issue, but no viable solutions, e.g. I could load a dialog from python but only from location=application not from location=user, weird.
OpenOffice 4.1.7 on Windows 10
User avatar
RoryOF
Moderator
Posts: 34612
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Python Dialog example

Post by RoryOF »

One of the first exercises when I learned programming nearly 60 years ago was to write a sorting program. Donald Knuth's "The Art of Computer Programming : Volume 3, Sorting and Searching" has comprehensive discussion on sorting algorithms.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
brauart
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Python Dialog example

Post by brauart »

Hello @Villeroy
thank you. For me thins only works for dialogs with the application, this one does work
dp.createDialog( "vnd.sun.star.script:Access2Base.dlgTrace?location=application")

it does not work my own dummy dialog - which is extremely simple.
dp.createDialog("vnd.sun.star.script:Standard.Dialog1?location=application")
dp.createDialog("vnd.sun.star.script:Standard.Dialog1?location=user")
OpenOffice 4.1.7 on Windows 10
brauart
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Python Dialog example

Post by brauart »

RoryOF wrote:One of the first exercises when I learned programming nearly 60 years ago was to write a sorting program. Donald Knuth's "The Art of Computer Programming : Volume 3, Sorting and Searching" has comprehensive discussion on sorting algorithms.
so I assume having the book you write all your software in assembler ? :knock:
OpenOffice 4.1.7 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Python Dialog example

Post by Villeroy »

My example code runs with any valid dialog URL I throw at it.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
RoryOF
Moderator
Posts: 34612
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Python Dialog example

Post by RoryOF »

brauart wrote:
RoryOF wrote:One of the first exercises when I learned programming nearly 60 years ago was to write a sorting program. Donald Knuth's "The Art of Computer Programming : Volume 3, Sorting and Searching" has comprehensive discussion on sorting algorithms.
so I assume having the book you write all your software in assembler ? :knock:
I have written in assembler and still could if I needed to; an algorithm is a method of approaching a problem, and can readily be adapted to any programming language.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Python Dialog example

Post by JeJe »

There is an associative array in Basic - the collection object. And you can search a string using regular expressions with Openoffice's XTextSearch.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Python Dialog example

Post by JeJe »

try location=document not location=user (if its in a document)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
brauart
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Python Dialog example

Post by brauart »

@Villeroy
YES, it finally works. And it does handle events and modify UI too! Thank you!! Great.

The entire small example looks like below. All it needs is a dialog: Standard.Dialog1 located in My Macros, only having a Button1.

Code: Select all

from __future__ import unicode_literals

import uno, unohelper
from com.sun.star.awt import XActionListener
from com.sun.star.awt import ActionEvent
from com.sun.star.lang import EventObject
from com.sun.star.ui.dialogs.ExecutableDialogResults import OK, CANCEL
import msgbox as util

_MY_BUTTON="Button1"
_MY_LABEL="Changed Label"
def Main(*args):
    ctx =XSCRIPTCONTEXT.getComponentContext()
    smgr = ctx.getServiceManager()
    dp = smgr.createInstanceWithContext("com.sun.star.awt.DialogProvider", ctx)
    ui = dp.createDialog("vnd.sun.star.script:Standard.Dialog1?location=application")
    ui.Title = "Python Dialog title from code"
    ctl = ui.getControl(_MY_BUTTON)
    ctl.Model.Label = _MY_LABEL
    act = ActionListener()
    ctl.addActionListener(act)
    ui.execute()                         
    ui.dispose()  # ui.endExecute
    ctl.removeActionListener(act)

class ActionListener(unohelper.Base, XActionListener):
    """ Listen to & count button clicks """
    def __init__(self):
        self.count = 0

    def actionPerformed(self, evt: ActionEvent):
        self.count = self.count + 1
        #mri(evt)
        MsgBox("Event received.")
        if evt.Source.Model.Name == _MY_BUTTON:
            evt.Source.Model.Label = _MY_LABEL+ str( self.count )
        return

    def disposing(self, evt: EventObject):  # mandatory routine
        pass

def MsgBox(txt: str):
    mb = util.MsgBox(uno.getComponentContext())
    mb.addButton("Ok")
    mb.show(txt, 0, "Python")

g_exportedScripts = (Main,)
OpenOffice 4.1.7 on Windows 10
brauart
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Python Dialog example

Post by brauart »

@JeJe
Thank you too for the support on OO Basic.
OpenOffice 4.1.7 on Windows 10
Post Reply