如何处理多个常见用户界面?
我正在开发一个在两个不同平台上运行的 python 应用程序,即常规桌面 linux 和 Maemo 4。我们在这两个平台上都使用 PyGTK,但在 Maemo 上有很多小调整,使其看起来不错,具体实现如下:
if util.platform.MAEMO:
# do something fancy for maemo
else:
# regular pygtk
大约需要 15 个 if 语句才能使 UI 在 Maemo 4 上看起来良好并正常工作。
这一直以来都非常易于管理。问题是,不久前发布了 Maemo 的新版本(5,又名 fremantle),与 Maemo 4 相比,它有一些很大的差异。我不想在整个 GUI 代码中添加一堆检查,以便让所有 3 个平台都使用相同的代码库很好地工作,因为那样会变得混乱。我也不想为每个平台创建原始 GUI 代码的副本,并简单地针对特定平台对其进行修改(我想重复使用尽可能多的代码)。
那么,有什么方法可以让基于相同核心 UI 代码的不同平台拥有稍微不同的 UI 呢?我不认为这是一个 python 或 Maemo 特定的问题,我只是想知道这是如何完成的。
I'm working on a python application that runs on 2 different platforms, namely regular desktop linux and Maemo 4. We use PyGTK on both platforms but on Maemo there are a bunch of little tweaks to make it look nice which are implemented as follows:
if util.platform.MAEMO:
# do something fancy for maemo
else:
# regular pygtk
There are roughly 15 of these if statements needed to get the UI looking and working nice on Maemo 4.
This has been very manageable for all this time. The problem is that a while ago there was a new version of Maemo released (5, aka fremantle) and it has some big differences compared to Maemo 4. I don't want to add a bunch of checks throughout the GUI code in order to get all 3 platforms working nicely with the same codebase because that would get messy. I also don't want to create a copy of the original GUI code for each platform and simply modify it for the specific platform (I'd like to re-use as much code as possible).
So, what are ways to have slightly different UIs for different platforms which are based on the same core UI code? I don't think this is a python or Maemo-specific question, I'd just like to know how this is done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在工厂中完成大部分工作:
然后,在代码的早期某个位置,您只需调用该工厂:
现在,在其他任何有条件的地方,您只需调用必要的函数:
只要
drawComboBox()< /code>,处理特定于平台的任何事情,你应该处于良好状态。
You could wind up much of this in a factory:
Then, somewhere early in your code, you just call that factory:
Now, everywhere else you had conditions, you just call the necessary function:
As long as
drawComboBox()
, handles anything specific to the platform, you should be in good shape.您可以将需要执行的特定于平台的操作隔离为
platform
模块内一致命名的小函数,使用您正在运行的平台创建正确的函数名称,然后getattr
正确的一个并调用它。 if/else 样板就会消失。You could isolate the platform specific stuff you need to do into small consistently named functions inside a
platform
module, create the right function name using the platform you're running on and thengetattr
the right one and call it. The if/else boilerplate would disappear then.我制作了一个单独的模块来处理普通 Linux、Maemo 4.1 和 Maemo 5 之间的所有专业知识。它检测可用的功能并允许程序正常降级。
例如
,更多信息请参见
Dialcentral、Gonert、ejpi 或 Quicknote 名为 hildonize.py 的文件的源代码
https ://garage.maemo.org/plugins/ggit/browse.php/?p=gc-dialer;a=blob;f=src/hildonize.py;
The One Ring 的 GObject Utils 的另一个示例 (go_utils.py)
I've made a separate module to handle all of my specializing between normal Linux, Maemo 4.1, and Maemo 5. It detects what features are available and allows the program to gracefully degrade.
For example
For more, see
Dialcentral, Gonert, ejpi, or Quicknote's source code for a file called hildonize.py
https://garage.maemo.org/plugins/ggit/browse.php/?p=gc-dialer;a=blob;f=src/hildonize.py;
Another example from The One Ring's GObject Utils (go_utils.py)