如何在 Python 应用程序之上创建 GUI,以便它可以执行 GUI 或 CLI?

发布于 2024-09-01 15:02:36 字数 337 浏览 5 评论 0原文

我正在尝试用 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 技术交流群。

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

发布评论

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

评论(4

有没有一种简单的方法可以检测 GTK 之类的东西,因此它只在 GTK 存在时应用代码?

首先,将您的应用程序分成 3 个独立的模块。

  1. 实际工作:foo_core.py

  2. 导入 foo_core 的 CLI 模块。将其命名为 foo_cli.py

  3. 导入foo_core的GUI模块。将其命名为 foo_gui.pyw

foo_cli 模块如下所示。

import foo_core
import optparse

def main():
    # parse the command-line options
    # the real work is done by foo_core

if __name__ == "__main__":
   main()

foo_gui 模块看起来像这样。

 import foo_core
 import gtk # or whatever

 def main()
     # build the GUI
     # real work is done by foo_core under control of the GUI

 if __name__ == "__main__":
     main()

这通常就足够了。值得信赖的是,人们可以自行决定需要 CLI 还是 GUI。

如果您想迷惑人们,可以编写一个 foo.py 脚本来执行如下操作。

try:
    import foo_gui
    foo_gui.main()
except ImportError:
    import foo_cli
    foo_cli.main()

is there a simple way of detecting something like GTK, so it only applied the code when GTK was present?

First, break your app into 3 separate modules.

  1. The actual work: foo_core.py.

  2. A CLI module that imports foo_core. Call it foo_cli.py.

  3. A GUI module that imports foo_core. Call it foo_gui.pyw.

The foo_cli module looks like this.

import foo_core
import optparse

def main():
    # parse the command-line options
    # the real work is done by foo_core

if __name__ == "__main__":
   main()

The foo_gui module can look like this.

 import foo_core
 import gtk # or whatever

 def main()
     # build the GUI
     # real work is done by foo_core under control of the GUI

 if __name__ == "__main__":
     main()

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.

try:
    import foo_gui
    foo_gui.main()
except ImportError:
    import foo_cli
    foo_cli.main()
兲鉂ぱ嘚淚 2024-09-08 15:02:36

这取决于您想要哪种互动。

如果您想要一个真正的 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.

一百个冬季 2024-09-08 15:02:36

我不建议使用 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.

我做我的改变 2024-09-08 15:02:36

好吧,我所做的是我有一个唯一的 bash 脚本,其中包含以下内容:

if [ "${0}" == "mcm" ]; then
    /usr/bin/python ${inst_dir}/terminal/mcm-terminal.py ${@}
else
    /usr/bin/python ${inst_dir}/gtk/mcm-gtk.py &
fi

然后我创建两个符号链接: /usr/sbin/mcm 和 /usr/bin/mcm-gtk

工作得非常好。

Well, what I do is that I have a one and only bash script with the following:

if [ "${0}" == "mcm" ]; then
    /usr/bin/python ${inst_dir}/terminal/mcm-terminal.py ${@}
else
    /usr/bin/python ${inst_dir}/gtk/mcm-gtk.py &
fi

Then I create two symlinks: /usr/sbin/mcm and /usr/bin/mcm-gtk

Works very nice.

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