使用浏览器作为界面的 Python 桌面应用程序?

发布于 2024-08-28 07:43:58 字数 247 浏览 5 评论 0原文

我想创建一个在用户计算机上运行的应用程序,一个独立的应用程序,带有安装和其他功能,但我希望界面是一个浏览器,可以是内部的并显示为操作系统窗口,也可以使用浏览器进行外部访问(即一些http服务器)。

原因是因为我对 Python 有一点了解,但我认为只要我有一些可以使用和操作的基本根,我就可以管理,这些根是 HTML、CSS 和 Javascript。

我还没有找到一个可以使用的好的 GUI 工具,并且总是在尝试混乱并最终一无所获后放弃这个想法。

I want to create an application that runs on the users computer, a stand-alone application, with installation and what-not, but I want the interface to be a browser, either internal and displayed as an OS window or external accessible using the browser (i.e. some http server).

The reason would be because I know a little about Python, but I think I can manage as long as I have some basic roots that I can use and manipulate, and those would be HTML, CSS, and Javascript.

I've yet to find a good GUI tool which I can use, and always abandon the idea after trying to mess around and eventually not getting anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一杆小烟枪 2024-09-04 07:43:58

Python 提供了您应该感兴趣的两件事:

  • 标准库中的 Web 服务器
  • 用于 Web 应用程序的标准化接口,称为 WSGI

因此,向应用程序添加 Web 接口相对容易。例如,在 Mercurial(版本控制系统)中,您有一个启动 Web 服务器的命令 hgserve

要查看 python 启动 Web 服务器和 WSGI 应用程序,只需执行以下操作:

python -m 'wsgiref.simple_server'

您可以查看 wsgiref 源代码或一些 WSGI 教程来制作一个简单的应用程序。

之后,您可能想使用 Web 框架(用于模板化和协作),但这是另一个问题......

Python offers two things that should be of your interest:

  • a web server in the standard library
  • a standartized interface for web applications, called WSGI

So it is relatively easy to add a web interface to your application. For example in Mercurial (the versioning system), you have a command hg serve that launches a web server.

To see python launching a web server, and a WSGI app, just do:

python -m 'wsgiref.simple_server'

You can look at the wsgiref source code or some WSGI tutorial to do a simple app.

After that, you may want to use a web framework (for templating & co), but that is another question...

倾听心声的旋律 2024-09-04 07:43:58

您可以使用睡衣。它是 Google Web Toolkit 到 Python 的端口,这基本上意味着您用 Python 编写,然后它会被编译为 HTML 和 JS。

You could use Pyjamas. It's a port of Google Web Toolkit to Python, which basically means you write in Python and it gets compiled to HTML and JS.

逐鹿 2024-09-04 07:43:58

有很多优秀的 GUI 工具可以满足您想要的 GUI 方式——HTML、CSS 和 Javascript。如果您不知道任何问题,请使用正确的标签提出单独的问题。

在这种安排中,Python 端不应该有自己的 GUI,而只是运行 Python 标准库的 HTTP 服务器的子类,仅提供 HTML、CSS 和 JS 文件,以及通过 JS 可以访问的其他 URL 上的 JSON 提供的数据通过 Ajax 技术实现,本质上是实现存储和业务逻辑 —— 因此,您可能想要使用什么“GUI 工具”还远不清楚?!

只需使用自己的“GUI 工具”单独开发Python 端(例如,使用IDLE、Wingware、SPE 或任何您喜欢的东西)和HTML/CSS/Javascript。毕竟,Python 对这些文件所做的只是静态地服务它们。

您可能会考虑使用一些 Python 端模板,例如 Mojo &c,但我的建议是避免这种情况:相反,始终采用“瘦服务器架构”,使 Python 端成为业务逻辑的 RESTful 服务器和存储层,并在浏览器中执行所有 GUI 工作。

There are plenty of excellent GUI tools for the way you want to do your GUI -- HTML, CSS, and Javascript. If you don't know of any, ask in a separate question with the right tags.

The Python side in such an arrangement should have no GUI of its own, but just run a subclass of the Python's standard library's HTTP server, just serving the HTML, CSS, and JS files, and data via JSON on other URLs that the JS can reach with Ajax techniques, essentially implementing storage and business logi -- so it's far from obvious what "GUI tool" you could possibly want for it?!

Just develop the Python side on its own (e.g. with IDLE, Wingware, SPE, or whatever you like) and the HTML / CSS / Javascript separately, with its own "GUI tool". All that Python will do with those files is statically serve them, after all.

You could be thinking of using some Python side templating, such as Mojo &c, but my recommendation is to avoid that: rather, go with the "thin server architecture" all the way, make the Python side a RESTful server of business logic and storage layers, and do all the GUI work in the browser instead.

同尘 2024-09-04 07:43:58

您是否仅仅因为无法使用 Python 小部件工具包(例如 Tkinter、wxpython 和 pyqt)而求助于 Web 浏览器?

您是否尝试过 Qt 设计器< /a>?它是一个图形 GUI 设计器,可以非常快速、轻松地开发美观的 GUI。它会随 PyQt 自动安装。

http://www.riverbankcomputing.co.uk/software/pyqt/download

Are you resorting to a web browser only because you've had difficulty with Python widget toolkits, like Tkinter,wxpython, and pyqt?

Have you tried Qt Designer? It's a graphical GUI designer, making it very quick and easy to develop great looking GUI's. It gets installed automatically with PyQt.

http://www.riverbankcomputing.co.uk/software/pyqt/download

友欢 2024-09-04 07:43:58

一个简单的 gui 演示如何在浏览器中使用 python 进行通信:

sqr.py

import http.server

class TestHandler(http.server.SimpleHTTPRequestHandler):

    def do_POST(self):
        """Handle a post request by returning the square of the number."""
        length = int(self.headers.get_all('content-length')[0])
        data_string = self.rfile.read(length)
        x = float(data_string)
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.flush_headers()
        self.wfile.write(str(x**2).encode())

server = http.server.HTTPServer(("", 8004), TestHandler)
server.serve_forever()

sqrt.html

<body>
<label for="fname">input x:</label>
<input type="text" id="fname" name="fname" onkeyup="runbuttonfunc()" value="4.0"><br>
<button id="runButton" onclick="runbuttonfunc()">=</button><br>
<label for="lname">output x*x:</label>
<input type="text" id="lname" name="lname"><br>

<script type="text/javascript">

function xml_http_post(url, data) {
    var req = new XMLHttpRequest();
    req.open("POST", url, true);
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            document.getElementById('lname').value = req.responseText
        }
    }
    req.send(data);
}

function runbuttonfunc() {
    xml_http_post("sqr.html", document.getElementById('fname').value)
}
</script>
</body>

使用python3 sqr.py运行程序,并在浏览器中使用地址http://localhost:8004/sqr.html

该示例源自如何在 Python 中实现 AJAX 的最小服务器?
http.server 是一个内置的在Python的模块中。然而,周围应该还有许多其他工具。

A simple gui demonstrating how to talk from the browser with python:

sqr.py

import http.server

class TestHandler(http.server.SimpleHTTPRequestHandler):

    def do_POST(self):
        """Handle a post request by returning the square of the number."""
        length = int(self.headers.get_all('content-length')[0])
        data_string = self.rfile.read(length)
        x = float(data_string)
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.flush_headers()
        self.wfile.write(str(x**2).encode())

server = http.server.HTTPServer(("", 8004), TestHandler)
server.serve_forever()

sqrt.html

<body>
<label for="fname">input x:</label>
<input type="text" id="fname" name="fname" onkeyup="runbuttonfunc()" value="4.0"><br>
<button id="runButton" onclick="runbuttonfunc()">=</button><br>
<label for="lname">output x*x:</label>
<input type="text" id="lname" name="lname"><br>

<script type="text/javascript">

function xml_http_post(url, data) {
    var req = new XMLHttpRequest();
    req.open("POST", url, true);
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            document.getElementById('lname').value = req.responseText
        }
    }
    req.send(data);
}

function runbuttonfunc() {
    xml_http_post("sqr.html", document.getElementById('fname').value)
}
</script>
</body>

Run the program with python3 sqr.py and use the address http://localhost:8004/sqr.html in the browser.

The example is derived from How to implement a minimal server for AJAX in Python?.
http.server is a built-in module of python. Yet, there should are many other tools around.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文