什么是“良好实践”?编写 Python GTK 的方法+应用?
我目前正在编写一个 PyGTK 应用程序,我想要一些关于构建我的应用程序的最佳方法的建议。基本上,应用程序将读取特定的文件规范并将其呈现在 GUI 中进行编辑。
目前我有一个 parser.py 来处理所有低级文件 IO 和文件解析。我在树视图中显示文件的内容,这意味着我需要使用树存储作为我的数据类型。
我遇到的问题是我只想到了解决这个问题的两种方法。第一个是我的解析器可以构建一个树存储并将其传递给我的 ui 类。这需要我的解析器依赖于 pygtk,并最大限度地减少类的潜在重用。第二个是在解析器中存储对我的 ui 类的引用,这也可能会限制我的解析器类作为独立库的重用。
将我的问题浓缩为一句话:有没有一种方法可以以一种更加Pythonic或OO友好的方式实现我的目标?
如果查看我的代码可以帮助任何人尝试回答我的问题: https://code.launchpad .net/~blainepace/nbtparser/trunk
欢迎其他Python建议,这是我的第一个Python程序,我可能会陷入更多C++风格的思维。我计划重构很多内容。
I'm currently writing a PyGTK application and I'd like some advice as to the best way to structure my application. Basically the application will read a specific file specification and present it in a GUI for editing.
Currently I have a parser.py which handles all the low level file IO and parsing of the file. I'm displaying the contents of the file in a treeview, which means that I need to use a treestore as my data type.
The problem I've ran into is that I've only thought of two solutions to this problem. The first is that my parser could build a treestore and pass it to my ui class. That requires my parser depending on pygtk, and minimizes the potential reuse for the class. The second would be storing a reference to my ui class in parser, which would also potentially limit the reuse of my parser class as a standalone library.
To condense my question into a short one liner: Is there a way to accomplish my goals in a more pythonic or OO-friendly way?
If looking at my code would help anyone trying to answer my question: https://code.launchpad.net/~blainepace/nbtparser/trunk
Other pythonic suggestions welcome, this is my first python program and I may be stuck in a more C++ style of thinking. I plan on refactoring a lot of it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该看一下教程 "Sub-在 Python 中对 GObject 进行分类”。这通过使用 GObject 的类型系统来创建信号和属性来实现,这允许您以易于与典型 PyGTK 语义集成的方式对底层行为进行建模(连接到信号、等待属性通知等)。
您的解析器和 UI 都应该只有要连接的属性和信号。然后,您有第三个类连接这些信号和回调,并在
if __name__ == __main__
块中启动主循环。通常,我的看起来像:
...然后在你的主脚本中:
当然,这通常会有所不同,具体取决于例如。我在使用格莱德吗?我是否为主应用程序子类化小部件或包装它们?这样
做的好处是,您可以编写一个测试解析器,它除了返回预编程的响应之外什么也不做,或者使用已知的测试文件。交换它就像更改上面一行一样简单:
You should take a look at the tutorial "Sub-classing GObject in Python". This goes through using GObject's type system to create signals and properties, which allow you to model underlying behavior in a way that is easy to integrate with typical PyGTK semantics (connecting to signals, waiting for property notifications, etc).
Both your parser and UI should have only properties and signals to connect to. You then have a third class that connects up these signals and callbacks and starts the main loop in a
if __name__ == __main__
block.Typically, mine look something like:
...and then in your main script:
Of course, this is often different depending on eg. am I using Glade? Do I subclass widgets for the main app or wrap them? etc.
The great thing about this is that you can then write, say, a test parser that does nothing but return pre-programmed responses, or uses a known test file. Swapping it in is as easy as changing one line above: