是否有检测GTK之类的简单方法,因此仅在存在GTK时才应用代码?
首先,将您的应用分为3个单独的模块。
实际工作:
foo_core.py
。导入的CLI模块
foo_core
。叫它foo_cli.py
。导入的GUI模块
foo_core
。叫它foo_gui.pyw
。
该
foo_cli模块如下所示。
import foo_coreimport optparsedef main(): # parse the command-line options # the real work is done by foo_coreif __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()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)