[Solved] Python Dialog example

Keyboard macros or custom scripts

[Solved] Python Dialog example

Postby brauart » Tue Jul 28, 2020 2:07 pm

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
brauart
 
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Python Dialog example

Postby JeJe » Tue Jul 28, 2020 6:35 pm

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.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1531
Joined: Wed Mar 09, 2016 2:40 pm

Re: Python Dialog example

Postby Villeroy » Wed Jul 29, 2020 11:07 am

Code: Select all   Expand viewCollapse view
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
User avatar
Villeroy
Volunteer
 
Posts: 29691
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Python Dialog example

Postby brauart » Wed Jul 29, 2020 11:50 am

@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
brauart
 
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Python Dialog example

Postby RoryOF » Wed Jul 29, 2020 11:59 am

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.9 on Xubuntu 20.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32521
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Python Dialog example

Postby brauart » Wed Jul 29, 2020 11:59 am

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

Postby brauart » Wed Jul 29, 2020 12:07 pm

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
brauart
 
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Python Dialog example

Postby Villeroy » Wed Jul 29, 2020 12:15 pm

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
Villeroy
Volunteer
 
Posts: 29691
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Python Dialog example

Postby RoryOF » Wed Jul 29, 2020 12:20 pm

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.9 on Xubuntu 20.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32521
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Python Dialog example

Postby JeJe » Wed Jul 29, 2020 12:42 pm

There is an associative array in Basic - the collection object. And you can search a string using regular expressions with Openoffice's XTextSearch.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1531
Joined: Wed Mar 09, 2016 2:40 pm

Re: Python Dialog example

Postby JeJe » Wed Jul 29, 2020 12:44 pm

try location=document not location=user (if its in a document)
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1531
Joined: Wed Mar 09, 2016 2:40 pm

Re: Python Dialog example

Postby brauart » Wed Jul 29, 2020 12:58 pm

@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   Expand viewCollapse view
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

Postby brauart » Wed Jul 29, 2020 1:05 pm

@JeJe
Thank you too for the support on OO Basic.
OpenOffice 4.1.7 on Windows 10
brauart
 
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 3 guests