如何在 Python 应用程序之上创建 GUI,以便它可以执行 GUI 或 CLI?
我正在尝试用 python 编写一个应用程序来使用串行控制电机。这一切在 CLI 情况下都可以正常工作并且通常是稳定的。但我想知道在这个代码库之上添加 GUI 有多简单?
我假设会有更多的代码,但是有没有一种简单的方法来检测像 GTK 这样的东西,所以它只在 GTK 存在时应用代码?
另外,一般来说,Python 中的 GUI 创建:最好在代码中保留尽可能少的 GUI 细节并使用类似 GTK 的基于 XML 的方法(使用 gtk.glade.XML() 函数)?是否有其他 GUI 工具包具有与 Glade/XML/“Explode in Code”方法类似的方法?
感谢您的任何建议。
安迪
I am trying to write an app in python to control a motor using serial. This all works in a CLI situation fine and is generally stable. but I was wondering how simple it was to add a GUI on top of this code base?
I assume there will be more code, but is there a simple way of detecting something like GTK, so it only applied the code when GTK was present?
Also, GUI creation in Python in general: is it best to keep as little GUI specifics out of the code and use something like GTK's XML based approach (using gtk.glade.XML() function)? Are there other GUI toolkits that have a similar approach to the Glade / XML / "Explode in Code" approach?
Thanks for any advice.
Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,将您的应用程序分成 3 个独立的模块。
实际工作:
foo_core.py
。导入
foo_core
的 CLI 模块。将其命名为foo_cli.py
。导入
foo_core
的GUI模块。将其命名为foo_gui.pyw
。foo_cli
模块如下所示。foo_gui
模块看起来像这样。这通常就足够了。值得信赖的是,人们可以自行决定需要 CLI 还是 GUI。
如果您想迷惑人们,可以编写一个 foo.py 脚本来执行如下操作。
First, break your app into 3 separate modules.
The actual work:
foo_core.py
.A CLI module that imports
foo_core
. Call itfoo_cli.py
.A GUI module that imports
foo_core
. Call itfoo_gui.pyw
.The
foo_cli
module looks like this.The
foo_gui
module can look like this.That's generally sufficient. People can be trusted to decide for themselves if they want CLI or GUI.
If you want to confuse people, you can write a
foo.py
script that does something like the following.这取决于您想要哪种互动。
如果您想要一个真正的 GUI,您可以使用 简陋对话框 模式来解耦来自程序逻辑的 GUI 内容,并使用文本“GUI”来处理 CLI。这还有一个很好的副作用,即 GUI 的大部分都可以进行可编程测试。
另一种方法是为 sys.stdin 和 sys.stout 分配自己的对象,这会将程序 IO 重定向到 GUI(这不适用于非 python 库)。这意味着 GUI 中的交互可能性更少,但编程工作量也更少。
It depends on which kind of interaction you want.
If you want a real GUI you can use the humble dialog pattern to decouple the GUI stuff from the program logic, and use a text "GUI" to handle the CLI. This also has the nice side-effect that big parts of the GUI get programmable testable.
Another way is to assign sys.stdin and sys.stout with own objects, which redirect your programs IO to the GUI (this does not work with non-python libraries). This means that you have fewer interaction possibilities in the GUI, but much less programming effort.
我不建议使用 XML 来制作 GUI。 XML 所做的只是为您提供一种描述布局的迷你语言。当你可以拥有Python的全部功能时,为什么还要使用迷你语言呢?
至于检测GTK,我不建议这样做。相反,添加一个命令行参数来确定是否创建 GUI(例如:myprogram -g)。然后,可以轻松创建桌面快捷方式或命令行别名以在 GUI 模式下启动,同时仍然能够从任何终端使用命令行工具。
但是,您确实希望将 GUI 代码与执行实际工作的代码分开。给自己一个包含所有业务逻辑的类,然后让 GUI 和 CLI 都访问该对象来完成工作。
I don't recommend doing a GUI in XML. All the XML does is give you a mini language for describing a layout. Why use a mini language when you can have the full power of python?
As for detecting GTK, I wouldn't suggest that. Instead, add a command line argument to determine whether to create a GUI or not (eg: myprogram -g). It then becomes easy to create a desktop shortcut or command line alias to start in GUI mode, while still being able to use the command line tool from any terminal.
You do, however, want to keep the GUI code separate from the bits that do the real work. Give youself a class that contains all the business logic, then have the GUI and CLI both access this object to do work.
好吧,我所做的是我有一个唯一的 bash 脚本,其中包含以下内容:
然后我创建两个符号链接: /usr/sbin/mcm 和 /usr/bin/mcm-gtk
工作得非常好。
Well, what I do is that I have a one and only bash script with the following:
Then I create two symlinks: /usr/sbin/mcm and /usr/bin/mcm-gtk
Works very nice.