什么是“良好实践”?编写 Python GTK 的方法+应用?

发布于 2024-09-17 23:07:03 字数 617 浏览 16 评论 0原文

我目前正在编写一个 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 技术交流群。

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

发布评论

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

评论(1

╰つ倒转 2024-09-24 23:07:03

您应该看一下教程 "Sub-在 Python 中对 GObject 进行分类”。这通过使用 GObject 的类型系统来创建信号和属性来实现,这允许您以易于与典型 PyGTK 语义集成的方式对底层行为进行建模(连接到信号、等待属性通知等)。

您的解析器和 UI 都应该只有要连接的属性和信号。然后,您有第三个类连接这些信号和回调,并在 if __name__ == __main__ 块中启动主循环。

通常,我的看起来像:

class MyApp(gtk.Window):

    def __init__(self, parser, ui):
        gtk.Window.__init__(self)
        parser.connect("some-signal", ui.update_this)
        parser.connect("some-other-signal", ui.update_that, extra_params)
        ui.connect("refresh-clicked", parser.reparse_file)
        self.add(ui)

...然后在你的主脚本中:

parser = parser.Parser(...)
ui = view.ParseView(...)
app = MyApp(parser, ui)

app.show_all()

gtk.main()

当然,这通常会有所不同,具体取决于例如。我在使用格莱德吗?我是否为主应用程序子类化小部件或包装它们?这样

做的好处是,您可以编写一个测试解析器,它除了返回预编程的响应之外什么也不做,或者使用已知的测试文件。交换它就像更改上面一行一样简单:

parser = parser.DummyParser(...)

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:

class MyApp(gtk.Window):

    def __init__(self, parser, ui):
        gtk.Window.__init__(self)
        parser.connect("some-signal", ui.update_this)
        parser.connect("some-other-signal", ui.update_that, extra_params)
        ui.connect("refresh-clicked", parser.reparse_file)
        self.add(ui)

...and then in your main script:

parser = parser.Parser(...)
ui = view.ParseView(...)
app = MyApp(parser, ui)

app.show_all()

gtk.main()

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:

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