android python scripting: GUI?
Asked Answered
P

3

10

Are there basic GUI functions in SL4A? I'd like to run a python program on Android and would need a listbox and simple dialogs (display info and get input).

There seem to be simple dialogs, but I haven't found a listbox. If there isn't a listbox, I should be able to create one if there's the ability to write text and highlight rectangles to specified parts of the screen and react to the user touching the screen or typing (including knowing where the user touched or where the cursor is).

Pebble answered 11/9, 2011 at 13:52 Comment(0)
G
11

Essentially there are three things you can do:

  1. If you just want simple Android lists and inputs, such as getting a user's input (e.g., a username and password) or showing a list of option to choose from, then there are some tutorials here: http://code.google.com/p/android-scripting/wiki/UiExamples

  2. If you want to show information (i.e., not have the user select it), you could try showing HTML in a WebView and respond via events: http://code.google.com/p/android-scripting/wiki/UsingWebView

    Doing this, you get limited functionality via events by using droid.eventPost(eventName,eventData); in JavaScript and droid.eventWaitFor(eventName).result in Python. You can then do whatever you want with the received data.

  3. If you're feeling brave, the latest unofficial release contains support for full Android layouts (ones made in XML, like those found in a native Android app). You can find a guide for that here: http://code.google.com/p/android-scripting/wiki/FullScreenUI

Gwendolyn answered 13/10, 2011 at 5:21 Comment(0)
A
10

If you want a python GUI solution on Android/iOS/Linux/Windows/Mac you can use kivy... its nice! kivy.org

Amparoampelopsis answered 6/7, 2012 at 8:42 Comment(1)
Based on browsing the documents, that looks great!Pebble
W
3

A fast and powerful option is to use REBOL 3. You can use SL4a features, but you don't need to:

http://business-programming.com/business_programming.html#section-18

Here are 10 fully functional demo programs, with GUIs. These run on Android AND desktop OSs, using the exact same code, without any changes. Teeny tiny scripts, and dead simple to create. There is nothing else like it:

REBOL []
load-gui
view [text "Hello World!"]


REBOL [title: "Tiny Note Editor"]
do %r3-gui.r3  ; download this file manually or just use load-gui as above
view [
    a1: area
    button "Save" on-action [write %notes.txt get-face a1]
    button "Load" on-action [set-face a1 to-string read %notes.txt]
]


REBOL [title: "Data Entry to CSV File"]
do %r3-gui.r3
view [
    text "First Name:"
    f1: field
    text "Last Name:"
    f2: field
    button "Submit" on-action [
        write/append %cntcts.txt rejoin [
            mold get-face f1 " " mold get-face f2 newline
        ]
        request "" "Saved"
    ]
    a1: area
    button "Load" on-action [set-face a1 to-string read %cntcts.txt]
]


REBOL [title: "Text File Reader (How to use a text list file selector)"]
do %r3-gui.r3
view [
    a1: area
    button "Load" on-action [
        files: read %./
        view/modal [
            text "File Name:"
            t2: text-list files on-action [
                set-face a1 to-string read(to-file pick files get-face t2)
                unview
            ]
        ]
    ]
]


REBOL [title: "List-View (Grid) Example"]
do %r3-gui.r3
view [
    text-table ["1" 200 "2" 100 "3"][
        ["asdf" "a" "4"]
        ["sdfg" "b" "3"]
        ["dfgh" "c" "2"]
        ["fghj" "d" "1"]
    ] 
]


REBOL [title: "Calculator"]
do %r3-gui.r3
stylize [
    btn: button [
        facets: [init-size: 50x50]
        actors: [on-action:[set-face f join get-face f get-face face]]
    ]
]
view [
    hgroup [
        f: field return
        btn "1"  btn "2"  btn "3"  btn " + "  return
        btn "4"  btn "5"  btn "6"  btn " - "  return
        btn "7"  btn "8"  btn "9"  btn " * "  return
        btn "0"  btn "."  btn " / "   btn "=" on-action [
            attempt [set-face f form do get-face f]
        ]
    ]
]


REBOL [title: "Sliding Tile Puzzle"]
do %r3-gui.r3
stylize [
    p: button [
        facets: [init-size: 60x60  max-size: 60x60]
        actors: [
            on-action: [
                t: face/gob/offset
                face/gob/offset: x/gob/offset
                x/gob/offset: t
            ]
        ]
    ]
]
view/options [
    hgroup [ 
        p "8"   p "7"   p "6"   return
        p "5"   p "4"   p "3"   return
        p "2"   p "1"   x: box 60x60 white
    ]
] [bg-color: white]


REBOL [title: "Math Test"]
do %r3-gui.r3
random/seed now
x: does [rejoin [random 10 " + " random 20]]
view [
    f1: field (x)
    text "Answer:"
    f2: field on-action [
        either (get-face f2) = (form do get-face f1) [
            request "Yes!" "Yes!"][request "No!" "No!"
        ]
        set-face f1 x
        set-face f2 ""
        focus f2
    ]
]


REBOL [title: "Minimal Cash Register"]
do %r3-gui.r3
stylize [fld: field [init-size: 80]]   
view [
    hgroup [
        text "Cashier:"   cashier: fld 
        text "Item:"      item: fld 
        text "Price:"     price: fld on-action [
            if error? try [to-money get-face price] [
                request "Error" "Price error" 
                return none
            ]
            set-face a rejoin [
                get-face a mold get-face item tab get-face price newline
            ]
            set-face item copy "" set-face price copy ""
            sum: 0
            foreach [item price] load get-face a [
                sum: sum + to-money price
            ]
            set-face subtotal form sum
            set-face tax form sum * .06
            set-face total form sum * 1.06 
            focus item
        ]
        return
        a: area 600x300
        return
        text "Subtotal:"   subtotal: fld 
        text "Tax:"        tax: fld 
        text "Total:"      total: fld
        button "Save" on-action [
            items: replace/all (mold load get-face a) newline " "
            write/append %sales.txt rejoin [
                items newline get-face cashier newline now/date newline
            ]
            set-face item copy "" set-face price copy "" 
            set-face a copy ""    set-face subtotal copy ""
            set-face tax copy "" set-face total copy ""
        ]
    ]
]


REBOL [title: "Requestors"]
do %r3-gui.r3
x: request/ask "Question" "Do you like this?."
either x = false [print "No!"] [print "Yes!"]
x: request/custom "" "Do you like this?" ["Yay" "Boo"]
either x = false [print "Boo!"] [print "Yay!"]
view [button "Click me" on-action[request "Ok" "You clicked the button."]]

If you want to use or add features from SL4a to REBOL, just do the code at https://raw.github.com/gchiu/Rebol3/master/protocols/prot-sl4a.r3

Wharfage answered 2/9, 2013 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.