android python脚本:GUI?

Posted

技术标签:

【中文标题】android python脚本:GUI?【英文标题】:android python scripting: GUI? 【发布时间】:2011-11-14 18:33:04 【问题描述】:

SL4A 中有基本的 GUI 功能吗?我想在 android 上运行一个 python 程序,需要一个列表框和简单的对话框(显示信息和获取输入)。

似乎有简单的对话框,但我还没有找到一个列表框。如果没有列表框,我应该能够创建一个列表框,如果有能力在屏幕的指定部分写入文本和突出显示矩形并对用户触摸屏幕或打字做出反应(包括知道用户触摸的位置或位置光标是)。

【问题讨论】:

【参考方案1】:

基本上你可以做三件事:

    如果您只想要简单的 Android 列表和输入,例如获取用户的输入(例如,用户名和密码)或显示可供选择的选项列表,那么这里有一些教程:http://code.google.com/p/android-scripting/wiki/UiExamples

    如果您想显示信息(即不让用户选择它),您可以尝试在 WebView 中显示 html 并通过事件响应:http://code.google.com/p/android-scripting/wiki/UsingWebView

    这样做,通过在 javascript 中使用 droid.eventPost(eventName,eventData); 和在 Python 中使用 droid.eventWaitFor(eventName).result,您可以通过事件获得有限的功能。然后,您可以对收到的数据做任何您想做的事情。

    如果您有勇气,最新的非官方版本包含对完整 Android 布局的支持(使用 XML 制作的布局,如原生 Android 应用程序中的布局)。您可以在此处找到相关指南:http://code.google.com/p/android-scripting/wiki/FullScreenUI

【讨论】:

【参考方案2】:

如果您想在 Android/ios/Linux/Windows/Mac 上使用 python GUI 解决方案,您可以使用 kivy...很好! kivy.org

【讨论】:

基于浏览文档,看起来很棒!【参考方案3】:

一个快速而强大的选项是使用 REBOL 3。您可以使用 SL4a 功能,但您不需要:

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

这里有 10 个功能齐全的演示程序,带有 GUI。它们在 Android 和桌面操作系统上运行,使用 完全相同 相同的代码,无需 任何 更改。很小的脚本,而且很容易创建。没有其他类似的

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."]]

如果您想使用或添加从 SL4a 到 REBOL 的功能,只需在https://raw.github.com/gchiu/Rebol3/master/protocols/prot-sl4a.r3 处执行代码

【讨论】:

以上是关于android python脚本:GUI?的主要内容,如果未能解决你的问题,请参考以下文章

将 Python 脚本与不同的 GUI 相结合

在不终止启动 Python 脚本的情况下关闭 pyqt5 GUI

双击时Python3 GUI脚本不起作用

在 Tkinter GUI 中显示 Python 脚本的输出

Python gui 表,其单元格是具有动态内容的下拉列表,其内容基于同一 gui 表的其他下拉列表单元格的内容

如何停止运行没有 GUI 窗口的 Python .pyw 脚本?